I need to support exact phrases (enclosed in quotes) in an otherwise space-separated list of terms. Thus splitting the respective string by the space-character is not sufficient anymore.
Example:
input : 'foo bar "lorem ipsum" baz'
output: ['foo', 'bar', 'lorem ipsum', 'baz']
I wonder whether this could be achieved with a single RegEx, rather than performing complex parsing or split-and-rejoin operations.
Any help would be greatly appreciated!
One that's easy to understand and a general solution. Works for all delimiters and 'join' characters. Also supports 'joined' words that are more than two words in length.... ie lists like
"hello my name is 'jon delaware smith fred' I have a 'long name'"
....A bit like the answer by AC but a bit neater...
... returns the array you're looking for.
Note, however:
replace(/^"([^"]+)"$/,"$1")
on the results.lorem
andipsum
, they'll be in the result. You can fix this by runningreplace(/\s+/," ")
on the results."
afteripsum
(i.e. an incorrectly-quoted phrase) you'll end up with:['foo', 'bar', 'lorem', 'ipsum', 'baz']
how about,
then do a pass on output to lose the quotes.
alternately,
then do a pass n output to lose the empty captures.
Thanks a lot for the quick responses!
Here's a summary of the options, for posterity:
For the record, here's the abomination I had come up with:
ES6 solution supporting:
Code:
Output:
If you are just wondering how to build the regex yourself, you might want to check out Expresso (Expresso link). It's a great tool to learn how to build regular expressions so you know what the syntax means.
When you've built your own expression, then you can perform a
.match
on it.