Split line on first appearance

I am trying to split the next line into two parts. Mostly because one of the possible delimiters is a whitespace character that can appear in the second capture group.

https://regex101.com/r/dS0bD8/1

How can I split these lines into \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"] 
+8
javascript regex
source share
2 answers
 (\skeyword\s|\skey\s|\s(?=.*[\[{]|[^\]}]+$)) 

Will work in all cases that you have given.
Demo is here .

+8
source share

You can replace "keyword" and "key" an empty string "" , then split \s+

 str.replace(/keyword|key/g, "").split(/\s+/) 
+3
source share

All Articles