2 title: require-unicode-regexp
7 RegExp `u` flag has two effects:
9 1. **Make the regular expression handling UTF-16 surrogate pairs correctly.**
11 Especially, character range syntax gets the correct behavior.
14 /^[👍]$/.test("👍") //→ false
15 /^[👍]$/u.test("👍") //→ true
18 2. **Make the regular expression throwing syntax errors early as disabling [Annex B extensions](https://www.ecma-international.org/ecma-262/6.0/#sec-regular-expressions-patterns).**
20 Because of historical reason, JavaScript regular expressions are tolerant of syntax errors. For example, `/\w{1, 2/` is a syntax error, but JavaScript doesn't throw the error. It matches strings such as `"a{1, 2"` instead. Such a recovering logic is defined in Annex B.
22 The `u` flag disables the recovering logic Annex B defined. As a result, you can find errors early. This is similar to [the strict mode](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode).
24 Therefore, the `u` flag lets us work better with regular expressions.
28 This rule aims to enforce the use of `u` flag on regular expressions.
30 Examples of **incorrect** code for this rule:
35 /*eslint require-unicode-regexp: error */
39 const c = new RegExp("ccc")
40 const d = new RegExp("ddd", "gi")
45 Examples of **correct** code for this rule:
50 /*eslint require-unicode-regexp: error */
54 const c = new RegExp("ccc", "u")
55 const d = new RegExp("ddd", "giu")
57 // This rule ignores RegExp calls if the flags could not be evaluated to a static value.
59 return new RegExp("eee", flags)
67 If you don't want to notify regular expressions with no `u` flag, then it's safe to disable this rule.