João Daniel
João Daniel

Reputation: 8986

Avoid regex to capture contents of string (between quotes)

I'm using the following regex to capture an array of string that follows certain conditions (e.g.: it's not prepended by some letter or number, and just contains strings surrounded by single or double quotes):

/^?[ =>]\[(('|")[^('|")\s]*('|")(, ?)?)+\]/

It should capture

["bla", "ble", "blo"]

However, it should not capture that, if it's part of a string between (single or double) quotes:

'["bla", "ble", "blo"]'

What should I add to avoid capturing those unwanted case?

Upvotes: 0

Views: 132

Answers (1)

bitwalker
bitwalker

Reputation: 9261

Add a negative lookbehind/lookahead expression to your pattern:

(?<!['])(?!['])(\[(('|")[^('|")\s]*('|")(, ?)?)+\])

Worked for me when testing, but it may depend on your regex engine.

Upvotes: 1

Related Questions