u
flag in regular expressions JS-0117151 };
152 }
153 const expr = expressions.compile(
154 tag.replace(/(’|‘)/g, "'").replace(/(“|”)/g, '"'),155 );
156 return {
157 get: function (scope, context) {
151 };
152 }
153 const expr = expressions.compile(
154 tag.replace(/(’|‘)/g, "'").replace(/(“|”)/g, '"'),155 );
156 return {
157 get: function (scope, context) {
It is recommended to use the u
flag with regular expressions.
The u
flag has two effects:
- It enables correct handling of UTF-16 surrogate pairs.
- It ensures the correct behavior of regex character ranges.
/^[👍]$/.test("👍") //→ false
/^[👍]$/u.test("👍") //→ true
For historical reasons, JavaScript regular expressions tolerate syntax errors.
For example, /\w{1, 2/
is a regex that would throw a syntax error, but JavaScript chooses not to.
It matches strings such as "a{1, 2"
instead.
This behaviour is defined in Annex B of the Javascript specification.
The u
flag disables the recovering logic Annex B
of the Javascript specification.
This way, you can find errors early.
It can therefore be thought of as a "strict mode" for RegEx literals.
This issue is raised when:
- A regular expression contains unicode property escapes i.e \p{<property-name>}
- A regular expression contains 4 bytes characters like emojis or some special characters
const a = /aaa/
const b = /bbb/gi
const c = new RegExp("ccc")
const d = new RegExp("ddd", "gi")
const a = /aaa/u
const b = /bbb/giu
const c = new RegExp("ccc", "u")
const d = new RegExp("ddd", "giu")
// This rule ignores RegExp calls if the flags are not a compile time constant.
function f(flags) {
return new RegExp("eee", flags)
}