最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Split string at first occurrence - Stack Overflow

programmeradmin0浏览0评论

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 and key 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
 |  Show 7 more ments

2 Answers 2

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+/)
发布评论

评论列表(0)

  1. 暂无评论