Home > database >  How do I do this regular expression without lookbehind (react native)
How do I do this regular expression without lookbehind (react native)

Time:07-13

It seems like when I add lookbehinds in my code, it fails to build

Sample strings:

 5. GET ME
 5 GET ME
 DONT GET ME

I want to get GET ME by making sure there is a 5 or 5. before it. Afterwards I'm expecting capitals, spaces, numbers and dashes.

With lookbehinds I'm using this:

/(?<=^5\. |^5 )[A-Z \d\-] $/g

How can I do this without lookbehind?

CodePudding user response:

You can match a 5 with an optional dot, and then capture in a group what follows.

Note that \s can also match a newline.

^5\.?\s ([A-Z\d-] (?:\s[A-Z\d-] )*)$

Regex demo

  • Related