I'm trying to split a sentence by whitespace/space but I must exclude space located inside parenthesis ()
, accolades {}
or squared brackets []
.
ex string: [apples carrots] (car plane train) {food water} foo bar
should result in an array containing:
Any ideas?
Not splitting, but matching and trimming. Example is in JavaScript, you can try it out in browser console:
var a = '[apples carrots] (car plane train) {food water} foo bar';
a.match(/[a-zA-Z0-9\[\]\(\){}]+/g).map(function (s) { return s.replace(/[\[\]\(\)\{\}]/, ''); });
["apples", "carrots", "car", "plane", "train", "food", "water", "foo", "bar"]
Alternatively:
a.split(/\s+(?![^\[]*\]|[^(]*\)|[^\{]*})/)
Produces:
["[apples carrots]", "(car plane train)", "{food water}", "foo", "bar"]
Split on whitespace followed by a positive look-ahead that checks if next bracket char (if any) is an open one (or end of input):
\s+(?=[^\])}]*([\[({]|$))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With