8

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.com/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"]
12
  • 1
    You want to use a single regex to match all the previous cases ? Commented Apr 25, 2016 at 23:38
  • What about taking a step back and removing all instances of keyword and key then using Regex? 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. Commented Apr 25, 2016 at 23:40
  • @Hamms the regex I have splits every space character :/ 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. Commented Apr 25, 2016 at 23:41

2 Answers 2

8
(\skeyword\s|\skey\s|\s(?=.*[\[{]|[^\]}]+$))

Will work for all the cases you gave.
Demo here.

Sign up to request clarification or add additional context in comments.

4 Comments

This is amazing. Thank you James! Unfortunately the string string case needs to include alpha-numeric and symbols not just word characters. How can I add this in? :) (\skeyword\s|\skey\s|\s(?=.*[\[{]|[a-z0-9-]+$))?
@JamesBuck hey, just found this fails {} {"can": []} you have any idea how to fix this?
@JamesBuck is there any way to only capture the first space? It's an odd bug.
@ThomasReggi you could try a negative lookahead instead to verify the space is not inside a {} or [] block - (\skeyword\s|\skey\s|\s(?![^[]+]$|[^{]+}$)). Regex demo. I don't know a way to check it's the first space without using a lookbehind or \K reset (both of which the JavaScript regex engine doesn't support).
3

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

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

1 Comment

@ThomasReggi You could alternatively replace keyword|key with comma character , then call .split(/,/)

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.