[英]split a string in javascript based on start and end delimiters
I'm looking for a way in Javascript to split a string into an array based on "starting" and "ending" separators rather than one separator, as str.split currently does.我正在寻找一种在 Javascript 中基于“开始”和“结束”分隔符而不是一个分隔符将字符串拆分为数组的方法,就像 str.split 目前所做的那样。
For example, if I have this string:例如,如果我有这个字符串:
const str = '{lang}_{cmp_abbrev}_{cmp_type}_{pl_abbrev}_{w}x{h}_d{dv}c{cv}'
The result of:的结果:
str.mySplit('{', '}');
would be this:会是这样的:
[
'{lang}',
'_',
'{cmp_abbrev}',
'_',
'{cmp_type}',
'_',
'{pl_abbrev}',
'_',
'{w}',
'x',
'{h}',
'_d',
'{dv}',
'c',
'{cv}'
]
Thus, it would take into consideration 2 characters instead of one character when determining how the string split should occur.因此,在确定如何进行字符串拆分时,它会考虑 2 个字符而不是 1 个字符。
Regex to the rescue!正则表达式来救援!
const str = '{lang}_{cmp_abbrev}_{cmp_type}_{pl_abbrev}_{w}x{h}_d{dv}c{cv}' const values = [...str.matchAll(/\w+|\{\w+\}/g)].flat() console.log(values)
Array#split
can take regular expressions with capturing groups: Array#split
可以采用带有捕获组的正则表达式:
'foo{bar}baz{}!'.split(/(\{.*?\})/g)
//=> ['foo', '{bar}', 'baz', '{}', '!']
Just be aware that empty strings can be generated eg,请注意,可以生成空字符串,例如,
'{foo}bar{baz}'.split(/(\{.*?\})/g)
//=> ['', '{foo}', 'bar', '{baz}', '']
'{foo}{bar}{baz}'.split(/(\{.*?\})/g)
//=> ['', '{foo}', '', '{bar}', '', '{baz}', '']
But that is both normal and to be expected.但这既是正常的,也是意料之中的。 If these are undesirable you can filter them out:
如果这些不受欢迎,您可以将它们过滤掉:
'{foo}{bar}{baz}'.split(/(\{.*?\})/g).filter(Boolean)
//=> ['{foo}', '{bar}', '{baz}']
With your initial string we have:使用您的初始字符串,我们有:
'{lang}_{cmp_abbrev}_{cmp_type}_{pl_abbrev}_{w}x{h}_d{dv}c{cv}'.split(/(\{.*?\})/g).filter(Boolean)
//=> ['{lang}', '_', '{cmp_abbrev}', '_', '{cmp_type}', '_', '{pl_abbrev}', '_', '{w}', 'x', '{h}', '_d', '{dv}', 'c', '{cv}']
If your example string always have that format, you could use split and capture 1 or more word characters in a group to keep in the result after the split.如果您的示例字符串始终具有该格式,则可以使用 split 并在组中捕获 1 个或多个单词字符以保留拆分后的结果。
Then assert a closing curly to the left, and opening curly to the right:然后向左断言一个闭合卷曲,向右打开一个卷曲:
(?<=})(\w+)(?={)
See a regex demo .查看正则表达式演示。
const str = '{lang}_{cmp_abbrev}_{cmp_type}_{pl_abbrev}_{w}x{h}_d{dv}c{cv}' const values = str.match(/{[^{}]*}|[^\s{}]+/g) console.log(values)
Another option is to match from {...}
or match 1+ non whitespace chars other than {
and }
using a negated character class :另一种选择是从
{...}
匹配或使用否定字符类匹配除{
和}
之外的 1+ 个非空白字符:
{[^{}]*}|[^\s{}]+
See another regex demo .查看另一个正则表达式演示。
const str = '{lang}_{cmp_abbrev}_{cmp_type}_{pl_abbrev}_{w}x{h}_d{dv}c{cv}' const values = str.match(/{[^{}]*}|[^\s{}]+/g) console.log(values)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.