1

For example, I have a target string like: str='Every "red","white","blue" flower is $7.'

If I run /Every ("(.+)",?)+ flower is \$(.+)/.exec(str), I will get "red","white","blue" as a whole output element, but what I want is ["red,"white","blue"].

Is there a way to do this, or I can only use split?

Thanks!

1
  • If the double-quotes are balanced, either lookbehind for " in a global .match while matching non-"s, or repeatedly exec "([^"]+)" and extract the first group Commented Nov 26, 2018 at 3:13

1 Answer 1

1

A global parameter RegExp and a String.match() solution would be the most practical:

str.match(/"(.*?)"/g) // [ '"red"', '"white"', '"blue"' ]
Sign up to request clarification or add additional context in comments.

3 Comments

Is this able to be modified to also capture other patterns like the tail $7?
@RomulusUrakagiTs'ai yes doing something like /("(.*?)")|(\$\d+)/g would catch that format as well
Hmm, this is a bit different from what I want to ask, but I think I can use the combination. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.