Home > Software engineering >  regex to match first space looking back from an = sign
regex to match first space looking back from an = sign

Time:05-27

I have a text like the below,

<p style="color: blue;" data="something">

I want to get style and data but /(?<=\s)(.*?)(?=\=)/g matches style and blue;" data.

Essentially, I want to only match the text between the first space and =

What am I missing? if someone can point me in the right direction, it'd be much appreciated.

CodePudding user response:

You can use

\S (?==)
[^\s=] (?==)

See the regex demo.

Details:

  • \S - one or more non-whitespace chars (even including = chars)
  • [^\s=] - one or more chars other than whitespace and = chars
  • (?==) - a positive lookahead that requires a = char to appear immediately to the right of the current location.
  • Related