For some reason those two regex act the same way:
"43\\gf..--.65".replace(/[^\d.-]/g, "");? // 43..--.65
"43\\gf..--.65".replace(/[^\d\.-]/g, "");? // 43..--.65
In the first regex I don't escape the dot(.
) while in the second regex I do(\.
).
What are the differences and why they act the same?
This question is related to
javascript
regex
replace
On this web page, I see that:
"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...
If you using JavaScript to test your Regex, try \\.
instead of \.
.
It acts on the same way because JS remove first backslash.
The dot operator .
does not need to be escaped inside of a character class []
.
Source: Stackoverflow.com