Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385295

jshint rejects regex - why?

# cat test.js
function foo() {
    var bmp = /^[\u{0020}-\u{007e}\u{00A0}-\u{FFFF}]+$/u;
}

# jshint test.js
test.js: line 2, col 15, Invalid regular expression.
test.js: line 2, col 56, Missing semicolon.
test.js: line 2, col 56, Expected an assignment or function call and instead saw an expression.

3 errors

# jshint -v
jshint v2.9.5

I've seen posts elsewhere about older versions of jshint rejecting valid regexes. Is this a case of that, or is something wrong with my expression? https://regex101.com claims that it is okay with the JavaScript engine in full unicode mode.

I have the same problem with:

var combine = /[\u{0020}-\u{007e}\u{00A0}-\u{0300}\u{036F}-\u{1AB0}\u{1AFF}-\u{1DC0}\u{1DCF}-\u{20D0}\u{20FF}-\u{FE20}\u{FE2F}-\u{FFFF}]/u;

For bonus points, if this is a jshint limitation, can I tell it to ignore this line? I have insufficient control over our build environment to upgrade to a later version of jshint.

Upvotes: 1

Views: 466

Answers (1)

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385295

It is true that jshint doesn't support ES6's Unicode-aware regular expressions (or \u{....} literals more generally) yet (at time of writing, the most recent release is v2.9.5).

See the following open issues:

Therefore, this can't work. I don't know whether you can suppress checking on this line without the parser just breaking more generally.

However, it turns out some of your target browsers (IE10 and IE11) don't support these features either, so the point is moot.


Workaround

Transpile the regexes into valid ES5 (using regexpu, or an online interface to the same), resulting in the following:

var bmp = /^(?:[ -~\xA0-\uD7FF\uE000-\uFFFF]|[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?:[^\uD800-\uDBFF]|^)[\uDC00-\uDFFF])+$/;

and:

var combine = /(?:[ -~\xA0-\u0300\u036F-\u1AB0\u1AFF-\u1DC0\u1DCF-\u20D0\u20FF-\uD7FF\uE000-\uFE20\uFE2F-\uFFFF]|[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?:[^\uD800-\uDBFF]|^)[\uDC00-\uDFFF])/;

Correctness yet to be entirely ascertained, but I have little reason to doubt the tool.

Upvotes: 2

Related Questions