Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
59
rated 0 times [  65] [ 6]  / answers: 1 / hits: 153801  / 12 Years ago, tue, may 1, 2012, 12:00:00

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?


More From » regex

 Answers
19

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.



[#85869] Sunday, April 29, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jackelyn

Total Points: 303
Total Questions: 103
Total Answers: 102

Location: Turks and Caicos Islands
Member since Sun, Mar 7, 2021
3 Years ago
jackelyn questions
Thu, Apr 8, 21, 00:00, 3 Years ago
Sun, Feb 28, 21, 00:00, 3 Years ago
Mon, May 25, 20, 00:00, 4 Years ago
Thu, Apr 30, 20, 00:00, 4 Years ago
;