[英]Split string using multi-character delimiters
I want to split a text using delimiters that can be longer than one symbol. 我想使用可以超过一个符号的定界符来分割文本。
For example, this should be split using and
, or
and comma: 例如,应使用and
, or
和逗号将其拆分:
"red lorry, yellow lorry and brown lorry".split(someRegexp)
should produce: 应该产生:
["red lorry", " yellow lorry", "brown lorry"]
It's not necessary for regex to trim spaces, this can be done later. 正则表达式不需要修剪空间,可以稍后进行。
You can use the regex 您可以使用正则表达式
/,|\b\s*(?:and|or)\s*\b/
Example 例
> "red lorry, yellow lorry and brown lorry".split(/,|\b\s*(?:and|or)\s*\b/)
< ["red lorry", " yellow lorry", "brown lorry"]
\\b
Matches word boundaries. \\b
匹配单词边界。 This ensure that the or
and and
are not matched within another word. 这样可以确保在另一个单词中的“ or
和“ and
不匹配。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.