Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect unsupported regex features at compile time

It seems safari has some regex features that are not well supported (lookbehind) even in it's latest version and throws some warnings:

register.js:298 SyntaxError: Invalid regular expression: invalid group specifier

Is there a way to detect, lint or transform those kind of regex automatically with webpack or any other tool at compile time, because those kind of errors tend to slip through tests?

In my case this is the regex causing an error

/(?<!\.|^)\.(?!\.+|$)/

It should match a dot in the middle of the string but not match a succession of dots, I'm trying to get a good equivalent for this regex but I am getting nowhere so far

const tests = [
'test.123.test',
'.test',
'test.',
'test.3..test',
];
//Expected
tests.forEach((test) => console.log(test.split(/(?<!\.|^)\.(?!\.+|$)/)));
//Actual
tests.forEach((test) => console.log(test.match(/^(([^.]*?)(?:\.)([^.]*?))*$/)));
like image 837
Tofandel Avatar asked Oct 23 '25 09:10

Tofandel


1 Answers

For the main part of my question, I found an eslint plugin that is able to detect ECMA features (including regex lookbehind) so by enabling the ones known to cause issues, because they can't be transpiled, it will automatically throw an error

{
    "plugins": ["es"],
    "parserOptions": {
        "ecmaVersion": 2018
    },
    "rules": {
        "es/no-async-iteration": "error",
        "es/no-malformed-template-literals": "error",
        "es/no-regexp-lookbehind-assertions": "error",
        "es/no-regexp-named-capture-groups": "error",
        "es/no-regexp-s-flag": "error",
        "es/no-regexp-unicode-property-escapes": "error"
    }
}
like image 102
Tofandel Avatar answered Oct 25 '25 23:10

Tofandel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!