简体   繁体   English

如何通过第一个数字出现将字符串分成两部分?

[英]How to split a string in two by first digit occurrence?

Having a string like this有这样的字符串

str = "word 12 otherword(s) 2000 19"

or like this或者像这样

str = "word word 12 otherword(s) 2000 19"

I need to split the string in two, in order to have an array like this:我需要将字符串分成两部分,以便拥有这样的数组:

newstr[0] = first part of the string (ie "word" in the first case, "word word" in the second case); newstr[0] = first part of the string (即第一种情况下的“word”,第二种情况下的“word word”);

newstr[1] = rest of the string (ie "12 otherword(s) 2000 19" in both cases). newstr[1] = rest of the string (即“12 otherword(s) 2000 19”在这两种情况下)。

I've tried to accomplish this using split and regex , without success:我尝试使用splitregex来完成此操作,但没有成功:

str.split(/\d.*/) returns Array [ "word ", "" ] or Array [ "word word ", "" ] str.split(/\d.*/)返回Array [ "word ", "" ]Array [ "word word ", "" ]

while str.split(/^\D*/gm) returns Array [ "", "12 otherword(s) 2000 19" ]str.split(/^\D*/gm)返回Array [ "", "12 otherword(s) 2000 19" ]

Would you give me a suggestion?你能给我一个建议吗? Even without using split and regex - if there's a better/faster (Vanilla JavaScript) solution.即使不使用splitregex - 如果有更好/更快的(Vanilla JavaScript)解决方案。

There's 3 things going on here.这里发生了 3 件事。

  1. String.split usually doesn't includes the matched delimiter in the return array. String.split 通常在返回数组中不包含匹配的分隔符。 So splitting abc.split('b') would return ['a', 'c'] .所以拆分abc.split('b')会返回['a', 'c'] This behavior can be changed by using a matching regex group;可以通过使用匹配的正则表达式组来更改此行为; ie adding parens 'abc'.split(/(b)/) will return ['a', 'b', 'c'] .即添加括号'abc'.split(/(b)/)将返回['a', 'b', 'c']

  2. String.split will keep the delimter seperate from the other elements. String.split 将使分隔符与其他元素分开。 Ie 'abc'.split(/(b)/) will return 3 elements ['a', 'b', 'c'] .'abc'.split(/(b)/)将返回 3 个元素['a', 'b', 'c'] Suffix the regex with .* to combine the last 2 elements: 'abc'.split(/(b.*)/) will return ['a', 'bc', ''] .使用.*为正则表达式添加后缀以组合最后两个元素: 'abc'.split(/(b.*)/)将返回['a', 'bc', '']

  3. Lastly, to ignore the last empty element, we send the 2nd param of 2 .最后,为了忽略最后一个空元素,我们发送2的第二个参数。

 let str = "word word 12 otherword(s) 2000 19"; let splitStr = str.split(/(\d.*)/, 2); console.log(splitStr);

You can match these parts:您可以匹配这些部分:

 const strs = ["word 12 otherword(s) 2000 19", "word word 12 otherword(s) 2000 19"]; for (var s of strs) { const [_, part1, part2] = s.match(/^(\D*)(\d+[\w\W]*)/) console.log([part1, part2]) }

See the regex demo .请参阅正则表达式演示

Regex details :正则表达式详细信息

  • ^ - start of a string ^ - 字符串的开头
  • (\D*) - Group 1: any zero or more chars other than digits (\D*) - 第 1 组:除数字以外的任何零个或多个字符
  • (\d+[\w\W]*) - Group 2: one or more digits, and then any zero or more chars as many as possible. (\d+[\w\W]*) - 第 2 组:一个或多个数字,然后是尽可能多的零个或多个字符。

Note you may .trim() the resulting parts when using them (print them with console.log([part1.trim(), part2.trim()]) ).请注意,您可以.trim()使用它们时生成的部分(使用console.log([part1.trim(), part2.trim()])打印它们)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM