Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In JavaScript, string '\m' is fully equals to string 'm', why? [duplicate]

console.log('\d' === 'd'); // true

Character 'd' is not a special character, why javascript want to slice the escape notation. It's better to keep the escape notation in my view.

When I want to fully match string-'\d' using regular expression, it just impossible! Taking the following code as an example.

console.log(RE.test('\d')); // it should log true 
console.log(RE.test('d'));  // it should log false              

Unfortunately, you just cannot figure out a regular expression pattern.

like image 804
quan lili Avatar asked Apr 07 '26 05:04

quan lili


1 Answers

You have no reason to escape d in a string and JavaScript ignores it. If you need \d you need to escape the escape character: \\d.

See also Why do linters pick on useless escape character?

like image 73
customcommander Avatar answered Apr 09 '26 19:04

customcommander



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!