首次出现时拆分字符串
Split string at first occurrence
我正在努力将以下字符串分成两部分。主要是因为可能的分隔符之一是空格字符,它可以出现在第二个捕获组中。
https://regex101.com/r/dS0bD8/1
如何在's
、'skeyword's
或'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"]
('skeyword's|'skey's|'s(?=.*['[{]|[^']}]+$))
将适用于您提供的所有情况。
在这里演示。
您可以将
"keyword"
和"key"
替换为空字符串""
,然后拆分's+
str.replace(/keyword|key/g, "").split(/'s+/)
相关文章:
- 通过跳过拆分字符在 javascript 中拆分字符串
- jQuery字符串使用split()方法在空格后拆分字符串
- Javascript-正则表达式,用于在未转义字符上拆分字符串,例如|,但忽略|
- 如何在JavaScript中按大小写拆分字符串
- 根据上次出现的、和_拆分字符串
- 拆分字符串数组(JavaScript)后未定义
- 在空行上拆分字符串
- 使用Regex拆分字符串,保留分隔符
- 使用jquery或regex,带点但不带点的拆分字符串位于单引号中
- Nashorn和Rhino:使用空白regexp拆分字符串
- Javascript:不使用.split()方法拆分字符串
- 如何使用正则表达式在javascript中拆分字符串
- Javascript在第一个空格上拆分字符串
- 首次出现时拆分字符串
- 如何使用正则表达式(.match())拆分字符串
- 使用正则表达式拆分字符串
- Javascript:拆分字符串,但仅使用 1 个结果
- Angularjs-拆分字符串问题
- angularjs在拆分字符串时解析语法错误
- 基于javascript中的AND OR逻辑拆分字符串