khinester
khinester

Reputation: 3520

Invalid regular expression: Invalid group in node.js

I am trying to map ELB load-balancer logs to have a common format like nginx and have this regex

const splitElbEntry = (elbLogEntry) => R.match(/(?P<date>[0-9-]+T[0-9:]+)\.\S+/)

I get this error:

SyntaxError: Invalid regular expression: /(?P<date>[0-9-]+T[0-9:]+)\.\S+/: Invalid group

where as on https://regexr.com/3o06l it is finding the timestamp, although if I add a new group the regex fails.

What will be the correct javascript equivalent for https://regex101.com/r/JOlrxS/5

Any advice is much appreciated

Upvotes: 1

Views: 4074

Answers (2)

khinester
khinester

Reputation: 3520

I got the required result using numbered capturing groups, thanks for the help.

https://regex101.com/r/JOlrxS/6

([0-9-]+T[0-9:]+)\.\S+\s+\S+\s+(\S+):\d+\s+\S+:\d+\s+\S+\s+(\S+)\s+\S+\s+(\S+)\s+\S+\s+\S+\s+(\S+)\s+\"\S+\s+\w+:\/\/([\w\-\.]*):\d+(\/\S*)\s+[^\"]+\"\s+\"([^\"]+)\"\s+\S+\s+\S+

Upvotes: 0

revo
revo

Reputation: 48711

The selected flavor in regexr is PCRE and your end flavor is JS. JS doesn't support (?P<name>...) notation. However, (?<name>...) would be implemented in ECMAScript 2018 (currently supported by Google Chrome).

You can't, as of now, use a named capturing group that works among all major browsers. Deal with simple numbered capturing groups.

Upvotes: 4

Related Questions