I have a string like the following.
我有一个像下面这样的字符串。
ids = 'S1486S1485E444'
I want to split it into an array of parts separated by the S or E like the following.
我想将它分成由S或E分隔的部分数组,如下所示。
["S1486", "S1485", "E444"]
This is what I came up with but it gives the undefined and empty strings as well.
这是我提出的,但它也提供了未定义和空字符串。
ids.split(/(S+\d+)|(E+\d+)/)
["", "S1486", undefined, "", "S1485", undefined, "", undefined, "E444", ""]
4 个解决方案
#1
3
Or just do a match:
或者只是做一个匹配:
'S1486S1485E444'.match(/([SE]+\d*)/g)
#2
3
You can use a lookahead to split on the character boundary where an S or E is the next character:
您可以使用前瞻分割字符边界,其中S或E是下一个字符:
var ids = 'S1486S1485E444';
var result = ids.split(/(?=S|E)/); // S1486,S1485,E444
The reason this works is that while .split
normally removes the character it matches on, this doesn't match on a character itself but a spot where the next character is the one you want.
这样做的原因是,虽然.split通常会删除它匹配的字符,但这与字符本身不匹配,而是下一个字符是您想要的字符。
#3
1
The argument to .split()
is the separator by which you want to divide your string. If that regex contains matching groups (denoted by parenthesis) then they are also included in the result.
.split()的参数是您想要分割字符串的分隔符。如果该正则表达式包含匹配的组(用括号表示),那么它们也包含在结果中。
One way would be similar to what you have, but then filter out all the empty strings.
一种方法类似于你拥有的,但然后筛选出所有空字符串。
ids.split(/([SE]\d+)/).filter(Boolean);
// result: ["S1486", "S1485", "E444"]
If your target browsers don't have .filter
on the Array prototype, you'll have to implement that for yourself, sorry. Alternatively, just get every second value from the result:
如果您的目标浏览器在Array原型上没有.filter,那么您必须自己实现,抱歉。或者,只需从结果中获取每秒值:
ids.split(/([SE]\d+)/)
// result: ["", "S1486", "", "S1485", "", "E444", ""]
From there, a simple for
loop could get you the parts you're after.
从那里,一个简单的for循环可以获得你所追求的部分。
#4
0
If you use the .match()
method it will return an array containing all matches:
如果使用.match()方法,它将返回一个包含所有匹配项的数组:
ids.match(/(S\d+|E\d+)/g)
// OR
ids.match(/([SE]\d+)/g)
Note that I've tweaked the regex a little and you need the g
flag for .match()
to return an array. If there are no matches it returns null
.
请注意,我已经调整了一点正则表达式,你需要.match()的g标志来返回一个数组。如果没有匹配则返回null。
#1
3
Or just do a match:
或者只是做一个匹配:
'S1486S1485E444'.match(/([SE]+\d*)/g)
#2
3
You can use a lookahead to split on the character boundary where an S or E is the next character:
您可以使用前瞻分割字符边界,其中S或E是下一个字符:
var ids = 'S1486S1485E444';
var result = ids.split(/(?=S|E)/); // S1486,S1485,E444
The reason this works is that while .split
normally removes the character it matches on, this doesn't match on a character itself but a spot where the next character is the one you want.
这样做的原因是,虽然.split通常会删除它匹配的字符,但这与字符本身不匹配,而是下一个字符是您想要的字符。
#3
1
The argument to .split()
is the separator by which you want to divide your string. If that regex contains matching groups (denoted by parenthesis) then they are also included in the result.
.split()的参数是您想要分割字符串的分隔符。如果该正则表达式包含匹配的组(用括号表示),那么它们也包含在结果中。
One way would be similar to what you have, but then filter out all the empty strings.
一种方法类似于你拥有的,但然后筛选出所有空字符串。
ids.split(/([SE]\d+)/).filter(Boolean);
// result: ["S1486", "S1485", "E444"]
If your target browsers don't have .filter
on the Array prototype, you'll have to implement that for yourself, sorry. Alternatively, just get every second value from the result:
如果您的目标浏览器在Array原型上没有.filter,那么您必须自己实现,抱歉。或者,只需从结果中获取每秒值:
ids.split(/([SE]\d+)/)
// result: ["", "S1486", "", "S1485", "", "E444", ""]
From there, a simple for
loop could get you the parts you're after.
从那里,一个简单的for循环可以获得你所追求的部分。
#4
0
If you use the .match()
method it will return an array containing all matches:
如果使用.match()方法,它将返回一个包含所有匹配项的数组:
ids.match(/(S\d+|E\d+)/g)
// OR
ids.match(/([SE]\d+)/g)
Note that I've tweaked the regex a little and you need the g
flag for .match()
to return an array. If there are no matches it returns null
.
请注意,我已经调整了一点正则表达式,你需要.match()的g标志来返回一个数组。如果没有匹配则返回null。