Escape dot in a regex range - javascript

Those two regex act the same way:
var str = "43gf\\..--.65";
console.log(str.replace(/[^\d.-]/g, ""));
console.log(str.replace(/[^\d\.-]/g, ""));
In the first regex I don't escape the dot(.) while in the second regex I do(\.).
What are the differences? Why is the result the same?

The dot operator . does not need to be escaped inside of a character class [].

Because the dot is inside character class (square brackets []).
Take a look at http://www.regular-expressions.info/reference.html, it says (under char class section):
Any character except ^-]\ add that character to the possible matches
for the character class.

If you using JavaScript to test your Regex, try \\. instead of \..
It acts on the same way because JS remove first backslash.

On regular-expressions.info, it is stated:
Remember that the dot is not a metacharacter inside a character class,
so we do not need to escape it with a backslash.
So I guess the escaping of it is unnecessary...

Related

Javascript regex for symbol but not space

I want to test a string for a symbol (e.g. $%^&*) so I use the following regex that works well:
/[\W+]/.test(string)
However, a space is also matched with this regex. What I really want is to test for a symbol but not a space. I'm trying the following code, but a space is still matched:
/[\W\S+]/.test(string)
Is there a better way to do this?
Since the whitespaces are included in \W, you need to use a negated character class:
[^\w\s]
However, you must clearly define what you call a "symbol" since this character class include for example accentued letters and all out of the ascii range.
\W is the equivalent of [^A-Za-z0-9_], meaning "any character except these." So you can use [^A-Za-z0-9_ ] (note the space at the end) to exclude spaces.
You may use the inverse shorthand character class for \W (which is \w) and use a negation in the character class:
/[^\w\s]+/.test(string)
Your regex [\W\S+] is also matching literal + as it is part of a character class. I think you need to place it outside the class to match 1 or more characters.
This regex may help you: [^\s\w]
https://regex101.com/r/jO4uU6/3
You may try this also.
(?!\s)\W
This would match any non-word character but not of a space.
/(?:(?!\s)\W)+/.test(string)
ie, match one or more non-word characters but not of a space.
DEMO

Match last string between special characters

I want to get the last string between special characters. I've done for square bracket as \[(.*)\]$
But, when I use it on something like Blah [Hi]How is this[KoTuWa]. I get the result as [Hi]How is this[KoTuWa].
How do i modify it to get the last stringthat is KotuWa.
Also, I would like to generalise to general special characters, instead of just matching the string between square brackets as above.
Thanks,
Sai
I would do this:
[^[\]]+(?=][^[\]]*$)
Debuggex Demo
To extend this to other types of brackets/special chars, say I also wanna match curly braces { and double quotes ":
[^{}"[\]]+(?=["\]}][^{}"[\]]*$)
Debuggex Demo (I added the multi-line /m only to show multiple examples)
Here is one way to do it:
\[([^\[]*)\]$
You can require that the string between brackets does not contain brackets:
Edit: thanks to funkwurm and jcubic for pointing out an error. Here's the fixed expression:
\[([^[]+)\][^\[]*$
If you need to use other separators than brackets, you should:
replace the \[ and \] with your new separators
replace the negative character classes with your beginning separator.
For example, assuming you need to use the separators <> instead of [], you'd do this:
<([^<]+)>[^\>]*$

Why doesn't [.\n]+ match the string 'a\nb'?

Here is my js Regex test.
'AAa\nbBB'.match(/AA[.\n]+BB/);//failed match
I thought [.\n]+ could match any characters. Am I wrong?
The dot matches a literal dot inside a character class.
Use 'AAa\nbBB'.match(/AA[\s\S]*BB/); instead.
In most regex flavors, you could set the /s flag to allow the dot to match newlines (for a regex like /AA.*BB/s). But in JavaScript, that feature is not available, so you need to use [\s\S] to match any character.

Regex not to start with "."

I have a regex for file name validation. Here it is:
/^[0-9a-zA-Z\^\&\'\#\{\}\[\]\,\$\=\!\-\#\(\)\.\%\+\~\_; ]+$/
How do I change it to check for file name not to start with . symbol. Thanks for help.
A negative lookahead will be the easiest solution:
/^(?!\.)[0-9a-zA-Z^&'#{}[\],$=!\-#().%+~_; ]+$/
Alternatively, you can match the first character with an extra character class:
/^[0-9a-zA-Z^&'#{}[\],$=!\-#()%+~_; ][0-9a-zA-Z^&'#{}[\],$=!\-#().%+~_; ]*$/
^^ no dot here
Btw, inside a character class nearly all special characters loose their function and will not need to be escaped.

How to remove square brackets in string using regex?

['abc','xyz'] – this string I want turn into abc,xyz using regex in javascript. I want to replace both open close square bracket & single quote with empty string ie "".
Use this regular expression to match square brackets or single quotes:
/[\[\]']+/g
Replace with the empty string.
console.log("['abc','xyz']".replace(/[\[\]']+/g,''));
str.replace(/[[\]]/g,'')
here you go
var str = "['abc',['def','ghi'],'jkl']";
//'[\'abc\',[\'def\',\'ghi\'],\'jkl\']'
str.replace(/[\[\]']/g,'' );
//'abc,def,ghi,jkl'
You probably don't even need string substitution for that. If your original string is JSON, try:
js> a="['abc','xyz']"
['abc','xyz']
js> eval(a).join(",")
abc,xyz
Be careful with eval, of course.
Just here to propose an alternative that I find more readable.
/\[|\]/g
JavaScript implementation:
let reg = /\[|\]/g
str.replace(reg,'')
As other people have shown, all you have to do is list the [ and ] characters, but because they are special characters you have to escape them with \.
I personally find the character group definition using [] to be confusing because it uses the same special character you're trying to replace.
Therefore using the | (OR) operator you can more easily distinguish the special characters in the regex from the literal characters being replaced.
This should work for you.
str.replace(/[[\]]/g, "");

Categories