Red
Red

Reputation: 7299

Regex not working in FireFox, how to make this Regex working in FireFox?

I have the following regex

string.match(/(?<notification_ID>\d+)/g)

In Chrome this works fine, but in FireFox this throws an exception

SyntaxError: invalid regexp group

I tried the solution given by JavaScript regular expression exception (Invalid group) by removing the ? mark.

So my Regex became

$location.path().match(/(<notification_ID>\d+)/g)

However, now it returns null.

What I want is to return an notification ID based on a string

Eg. https://example.com/here-comes-a-path/#!/notification-14 should return 14

How can I make the regex so it will work among all browsers?


If you are using Chrome this snippet will work

var string = 'https://example.com/here-comes-a-path/#!/notification-14';

console.log(string.match(/(?<notification_ID>\d+)/g))

Upvotes: 0

Views: 1513

Answers (2)

Manuel Mannhardt
Manuel Mannhardt

Reputation: 2201

var string = 'https://example.com/here-comes-a-path/#!/notification-14';

console.log(string.match(/notification-(\d+)/g))

Thats what you have to use so it works in all browsers. Named groups are new to JS as of ES2018, see here.

Firefox has not implemented it yet, see their bug report here.

Also see this post for more informations.

Upvotes: 1

Phillip
Phillip

Reputation: 6253

A regex like this would work:

notification-(\d+)

It matches notification- and then captures one or more digits in (\d+).

const regex = /notification-(\d+)/
const string = "https://example.com/here-comes-a-path/#!/notification-14"

const matches = string.match(regex);
console.log(matches[1]);

Upvotes: 2

Related Questions