Works in Chrome, But Breaks in Safari: Invalid Regular Expression: Invalid Group Specifier Name /(<=\/)([^#]+)(=#*)/

Works in Chrome, but breaks in Safari: Invalid regular expression: invalid group specifier name /(?=\/)([^#]+)(?=#*)/

Looks like Safari doesn't support lookbehind yet (that is, your (?<=\/)). One alternative would be to put the / that comes before in a non-captured group, and then extract only the first group (the content after the / and before the #).

/(?:\/)([^#]+)(?=#*)/

Also, (?=#*) is odd - you probably want to lookahead for something (such as # or the end of the string), rather than a * quantifier (zero or more occurrences of #). It might be better to use something like

/(?:\/)([^#]+)(?=#|$)/

or just omit the lookahead entirely (because the ([^#]+) is greedy), depending on your circumstances.

Works in Chrome, but breaks in Safari: Invalid regular expression: invalid group specifier name /(?=\/)([^#]+)(?=#*)/

Looks like Safari doesn't support lookbehind yet (that is, your (?<=\/)). One alternative would be to put the / that comes before in a non-captured group, and then extract only the first group (the content after the / and before the #).

/(?:\/)([^#]+)(?=#*)/

Also, (?=#*) is odd - you probably want to lookahead for something (such as # or the end of the string), rather than a * quantifier (zero or more occurrences of #). It might be better to use something like

/(?:\/)([^#]+)(?=#|$)/

or just omit the lookahead entirely (because the ([^#]+) is greedy), depending on your circumstances.

React application on Safari throws SyntaxError: Invalid regular expression: invalid group specifier name

Even if similar questions exist, and the root cause has been already recognized as the missing Safari support for lookbehind regex, I would like to provide a general way to handle those situations where, as described in the main question, you can not just fix a line of code - for example when the issue is caused by an external library.

How to handle broken external depencencies

In my case, the bug had been introduced with draft-js-utils 1.4.1, so I solved it downgrading to the first know working version (1.4.0). In order to achieve this, I edited the package.json file changing the dependency line from

"draft-js-utils": "^1.4.0"

to

"draft-js-utils": "1.4.0"

TIP: Avoiding the caret range, you can stick it to a specific version.

How to find broken external depencencies

The only way to find out what dependencies have been affected by this bug is to look for the error message in Github/Gitlab search - currently almost 300 public repositories have a related issue opened.

The hardest thing about this bug is that it could be hidden inside transitive dependencies.
You might not even know you are using that package.

If you are not lucky enough to spot it using a Github/Gitlab search, you could try with a local search using your IDE or grep. You need to look for the lookbehind symbols ?<!:

grep -r "?<\!" node_modules

Being a last resort, this approach could be either very slow or produce a huge-and-hard-to-read output.



A sad note

It looks like Webkit developers are not going to add lookbehind regex support soon - the issue has been created in July 2017 without receiving attention from them. Moreover, even if the Safari's issue has been recognized and tracked, no polyfill exists to fix it at the build level (e.g. using Babel).

Regular expression to get a string between two strings in Javascript

A lookahead (that (?= part) does not consume any input. It is a zero-width assertion (as are boundary checks and lookbehinds).

You want a regular match here, to consume the cow portion. To capture the portion in between, you use a capturing group (just put the portion of pattern you want to capture inside parenthesis):

cow(.*)milk

No lookaheads are needed at all.

This regular expression breaks on safari but works in Google Chrome, how do I make it work in both

Use

let check = 'layout=';
let xml = "change layout = ";
let regex = /layout\s*=(?!=)\s*/g;
xml = xml.replace(regex, check);
console.log(xml)


Related Topics



Leave a reply



Submit