JY.Zhao
JY.Zhao

Reputation: 21

regExp weird reaction in Chrome console

as the following regExp:

var reg = /(\.[\u4e00-\u9fa5A-Z_])+/i;
reg.test('.a.s');

only the Chrome get false, other browsers and node all get true.

enter image description here

Is that because the Chrome regExp engine?


This problem was solved thanks to the anser of @Wiktor Stribiżew. Please refer to his answer.

======================================================================

This may be a bug of ES6 when perform RegExp. Someone has report the issue in both node and chrome community. Refer to these:

https://github.com/nodejs/node/issues/7708?plg_nld=1&plg_uin=1&plg_auth=1&plg_nld=1&plg_usr=1&plg_vkey=1&plg_dev=1

https://bugs.chromium.org/p/v8/issues/detail?id=5199

Upvotes: 2

Views: 133

Answers (2)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626747

Latest Chrome versions run on V8 that is ES6 compliant.

So, the \u9fa5A is treated as one code point rather than \u9fa5 and A, see this test:

 // Chorme 51.0.2704.103 m output is given on the right
console.log(
  String.fromCharCode(parseInt("4e00", 16)), //  => "一"
  String.fromCharCode(parseInt("9fa5A", 16)), // => "署"
  String.fromCharCode(parseInt("9fa5", 16)) //  =>  "龥"
);

You need to make sure the \u values are parsed correctly with \u{XXXX} notation and /u modifier (will work with ES6 only) or rearrange the character class parts as shown below:

console.log(/(\.[\u{4e00}-\u{9fa5}A-Z_])+/iu.test('.a.s'));
// or rearrange the ranges:
console.log(/(\.[A-Z_\u4e00-\u9fa5])+/i.test('.a.s'));

Upvotes: 3

Praveen Srinivasan
Praveen Srinivasan

Reputation: 1620

I have checked with the result. I have got only true result. Do you have any particular version or something?

I am getting the true result only

Upvotes: 0

Related Questions