I have a string like this: ----------- 243f,33f----
Now I want to remove all the - chars except the first -, the , and the numbers. The result should be -243,33. How would I do this?
Your question still isn't very clear, but this yields the output you want:
'----------- 243f,33f----'.replace(/(?!^-)[^\d,]+/g, '')
The regex matches one or more of any character except digits or commas, after making sure the first character isn't a hyphen at the beginning of the string.
EDIT: To those who came up with regexes using (?<!^) and then withdrew them because JavaScript doesn't support lookbehinds, that wasn't necessary. I know there were other problems with those answers, but for the purpose of matching "not the beginning of the string", (?!^) works just fine.
'----------- 243f,33f----'.replace(/[^-0123456789,]/g, '').replace(/^-/, 'x').replace(/-/g, '').replace(/x/, '-')
output: '-243,33'
replace the substring from second character ala
string.charAt(0) + string.substring(1).replace("-","");
but i don't know if the syntax is correct
EDIT:
oh if you want to remove the f's too you can remove any nondigit and non-comma:
string.charAt(0) + string.substring(1).replace("[^0-9,]","");
Try this one:
(?:(^-)|[^\d,\r\n])
and replace with
$1
See it here on Regexr
Its a bit more difficult since Javascript does not support look behinds. So if there is a - at the start of the line it will be replaced by itself, for the other matches $1 is empty so replaced with nothing.
Related
I have this string:
++some+text+to+replace+stay,++some+text+to+replace+stay,++some+text+to+replace+stay
I want to replace everything between the + characters where no , is included.
So the result should look like:
_stay,_stay,_stay
("_" is the replaced space)
I've already played around a bit and got this working: (\+[^\,]+)(?=[^,](?:,|$))
Debuggex Demo
This is the closest I can get to the right result with my regex knowledge. Could you please help me out and explain your solution?
You started out right, but complicated everything. Simply:
\+[^,]+\+
A plus sign (\+), followed by anything that isn't a comma ([^,]+), followed by a plus sign (\+).
s.replace(/\+[^,]+(.{4})(?=,|$)/g, '_$1') // where `s` is the string
I am trying to build a RegEx in Javascript that does not match if a certain string appear. So I want to match this curly bracket { but only if in front of it is not the string else.
I am trying to do this ^ *[^else]* *{.*$ but in fact this doe not match if any character in elsestring appear, for example this does not match also this:
erai {
I want to match all the cases when { appear despite of this case else {.
Please could you help me. Here is my DEMO
You can use a negative lookahead. This is supported by JavaScript:
(?!\s*else).+ *({).*$|
DEMO
JavaScript RegEx doesn't support ifs but we can use a trick for it to work:
(?!RegExp)
That's the first part, if RegExp (which is a regex) doesn't appear, then we do the code after that:
.+ *({).*$
That's the RegEx we run. Broken does, it is:
.+ Match anything
* Until 0 - unlimited spaces
({) Capture the {
.*$ Match anything till the end
Now this won't work unless we add a | at the end, or an OR. This will trick it into working like an if statement
Debuggex Demo
What you are looking for is called negative lookbehind.
I had the following regex to match opening quotation marks:
/'(?=\b)/g
But then I realized that it also captures stuff like don't and it's.
So I added another rule:
/'(?=\b[^a-zA-Z])/g
Capture an opening quotation mark not followed by a letter (t and s in this case). But now none of the quotation marks are being highlighted.
Did I modify the regex in a wrong way?
EDIT:
Oh, I realized my dumb mistake. Anyway, here's what I want to do:
"two 'three'"
"four don't it's "
I want to match the opening ' in three but not the 's in don't and it's
You can try this regex to match quotes but skip those cases like don't it's etc:
/(?:^|[^a-zA-Z])'|'(?![a-zA-Z])/gm
RegEx Demo
You can use the following regex to only match opening '
/(?!\b)'/g
See the demo on your testcases
You have your look-around in the wrong place. You want to find instances where a quote is preceded by nothing, and proceeded by characters. This would probably work
/\b'\B/
Referring to your last EDIT, this Regex /[\.\,\;\?\!\s]+'/g is what you are looking for, it matches quotation in sentences like 'three' and skips what you need (sentences like don't and it's).
Here's a DEMO.
I just wanted to ask for an example of a string that would match this regular expression for JS:
/\/[a-z]{2,6}\/(\([0-9]+\)?$/
But this bit confuses me: /(\([0-9]+\)?$/
If I could get an example of a string using this regular expression and a brief explanation, that would be enough to clear it up for me.
Thanks!
EDITED: Sorry for the trouble, I missed a parentheses, however I just want to clear up, a string that would match would be such as:
/ab/(12345) or /abcdef/(1) etc right?
Here is a visual representation of the #Paul Roub explanation:
EDITED with last pattern
\/[a-z]{2,6}\/\([0-9]+\)$
Debuggex Demo
This example is OK : /abcdef/(12345)
\/
A forward slash (escaped), followed by
[a-z]{2,6}
between two and six lowercase English letters, followed by
\/
another slash
([0-9]+)?
the inside part - one or more digits. ? means "zero or one", so we're looking for a string of digits, or nothing. The parentheses would let this number be captured as a group for later processing
$
and the end of the string.
Things that would match:
/ab/0
/ab/
/acdefg/12345
things that would not match
/a/0
/abcdefgh/12345
/ab/0x
That regexp is malformed.
Opening bracket is escaped, but closing one not
Opening paren is not escaped but closing one is
I think the intended regexp was:
/\/[a-z]{2,6}\/([0-9]+)?$/
This would match:
/ab/1
/ab/
/abc/123
Wouldn't match:
/a/1
ab/1
/abcdefg/123
Cheers.
For some reason, I can't seem to find a good answer for this one.
I have been trying to escape out the caret (\^), and to use the hex, octal, and other codes for the character using \xdd, \dddd, etc...
But my replace regexp won't replace the caret (^) with anything. It seems to simply break the expression.
Here is the code I am using:
var field,myExp;
// \x5E is supposed to represent the caret in Hex...
myExp = / *[^a-z^A-Z^0-9\s\x5E]/gi;
field = field.replace(myExp,"");
alert(field);
Help!
The code snippet you gave is rather confusing, but based on the title of the question, if you just want to replace the character ^ with something else, that can be achieved like this...
var str1 = "test^123";
var str2 = str1.replace(/\^/g, "\x005E");
alert(str2);
A character group beginning with ^ is an exclusionary group, and will match every character that isn't in the [].
If you're trying to remove any letter, number, or ^, change the regex to
myExp = / *[a-zA-Z0-9^\s]/gi;
When you have the caret as the first character in a [] set, it means "not" - but only at the start. So your regexp means "(spaces followed by) anything that's not a-z, or caret, or A-Z, or caret, or 0-9 etc". Remove all but the first caret and you may have more luck :-)
I found the answer, but you guys all helped me get there. Thanks!
I think what was happening was that my exlude (^) was used too many times and so was creating an exclusion of my exclusionary groups... Since there were no separators between the groups, the first one does the trick.
ORIGINAL:
repExp = / *[^a-z^A-Z^0-9]/gi;
FINAL REGEXP:
repExp = / *[^a-zA-Z0-9]/gi;
The above filters out anything that is not a leter (a-zA-Z) or number (0-9) from a string.
Thanks, people!
P.S. The space after the initial "/" is there because for some reason, Dreamweaver sees it as the beginning of a comment. :-(
Are you trying to replace or keep all a-z, A-Z, 0-9, whitespace, and carats?
If you're trying to keep them, only use one ^ at the very beginning of the expression like so:
[^a-zA-Z0-9\s^]
If you're trying to replace them all including carat, use:
[a-zA-Z0-9^\s]
Edit (updated answer in response to comment):
Use
[^a-zA-Z0-9]
to match and replace all characters that are not a-z, A-Z, 0-9.
Note: You should use the same expression server-side to validate these form fields, as people could have javascript disabled, or just mess with the POST value to mess with your database.