u
flag in regular expressions JS-0117 5747/*! @drupal/once - v1.0.1 - 2021-06-12 */
5748var once = (function () {
5749 "use strict";
5750 var n = /[\11\12\14\15\40]+/, 5751 e = "data-once",
5752 t = document;
5753 function r(n, t, r) {
5537 };
5538 },
5539 );
5540 var Xt = /^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g; 5541 (S.proxy = function (e, t) {
5542 var n, r, i;
5543 if (("string" == typeof t && ((n = e[t]), (t = e), (e = n)), m(e)))
2472 var ce,
2473 fe,
2474 pe = /^(?:checkbox|radio)$/i,
2475 de = /<([a-z][^\/\0>\x20\t\r\n\f]*)/i, 2476 he = /^$|^module$|\/(?:java|ecma)script/i;
2477 (ce = E.createDocumentFragment().appendChild(E.createElement("div"))),
2478 (fe = E.createElement("input")).setAttribute("type", "radio"),
1782 };
1783 },
1784 );
1785 var P = /[^\x20\t\r\n\f]+/g; 1786 function R(e) {
1787 return e;
1788 }
1579 function A(e, t) {
1580 return e.nodeName && e.nodeName.toLowerCase() === t.toLowerCase();
1581 }
1582 var N = /^<([a-z][^\/\0>:\x20\t\r\n\f]*)[\x20\t\r\n\f]*\/?>(?:<\/\1>|)$/i; 1583 function j(e, n, r) {
1584 return m(n)
1585 ? S.grep(e, function (e, t) {
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)
}