I'm struggling splitting the following string into two pieces. Mainly because the one of the possible delimiters is a space character, which can appear in the second capture group.
How can I split these strings at either \s
, \skeyword\s
, or \skey\s
?
'[] []' // => ['[]', '[]']
'[] keyword []' // => ['[]', '[]']
'[] key []' // => ['[]', '[]']
'[] ["can contain spaces"]' // => ['[]', '["can contain spaces"]']
'[] keyword ["can contain spaces"]' // => ['[]', '["can contain spaces"]']
'[] key ["can contain spaces"]' // => ['[]', '["can contain spaces"]']
'{} {}' // => ['{}', '{}']
'{} keyword {}' // => ['{}', '{}']
'{} key {}' // => ['{}', '{}']
'{} {"can":"contain spaces"}' // => ['{}', '{"can":"contain spaces"}']
'{} keyword {"can":"contain spaces"}' // => ['{}', '{"can":"contain spaces"}']
'{} key {"can":"contain spaces"}' // => ['{}', '{"can":"contain spaces"}']
'string string' // => ["string", "string"]
'string keyword string' // => ["string", "string"]
'string key string' // => ["string", "string"]
I'm struggling splitting the following string into two pieces. Mainly because the one of the possible delimiters is a space character, which can appear in the second capture group.
https://regex101./r/dS0bD8/1
How can I split these strings at either \s
, \skeyword\s
, or \skey\s
?
'[] []' // => ['[]', '[]']
'[] keyword []' // => ['[]', '[]']
'[] key []' // => ['[]', '[]']
'[] ["can contain spaces"]' // => ['[]', '["can contain spaces"]']
'[] keyword ["can contain spaces"]' // => ['[]', '["can contain spaces"]']
'[] key ["can contain spaces"]' // => ['[]', '["can contain spaces"]']
'{} {}' // => ['{}', '{}']
'{} keyword {}' // => ['{}', '{}']
'{} key {}' // => ['{}', '{}']
'{} {"can":"contain spaces"}' // => ['{}', '{"can":"contain spaces"}']
'{} keyword {"can":"contain spaces"}' // => ['{}', '{"can":"contain spaces"}']
'{} key {"can":"contain spaces"}' // => ['{}', '{"can":"contain spaces"}']
'string string' // => ["string", "string"]
'string keyword string' // => ["string", "string"]
'string key string' // => ["string", "string"]
Share
Improve this question
asked Apr 25, 2016 at 23:34
ThomasReggiThomasReggi
59.6k97 gold badges258 silver badges459 bronze badges
12
- 1 You want to use a single regex to match all the previous cases ? – ismnoiet Commented Apr 25, 2016 at 23:38
-
What about taking a step back and removing all instances of
keyword
andkey
then using Regex? – Sumner Evans Commented Apr 25, 2016 at 23:40 - What's wrong with the regex you have? Note that this tool is using a test STRING singular, and is therefore assuming that everything in the input is one long string, when you are intending for them to be multiple individual strings. – Hamms Commented Apr 25, 2016 at 23:40
- @Hamms the regex I have splits every space character :/ – ThomasReggi Commented Apr 25, 2016 at 23:40
- @SumnerEvans I'm curious how you can have or options and only match the first instance. It's apart of the question. – ThomasReggi Commented Apr 25, 2016 at 23:41
2 Answers
Reset to default 8(\skeyword\s|\skey\s|\s(?=.*[\[{]|[^\]}]+$))
Will work for all the cases you gave.
Demo here.
You can replace "keyword"
and "key"
with empty string ""
, then split \s+
str.replace(/keyword|key/g, "").split(/\s+/)