我需要在nodejs中带点后面的数字后拆分一个字符串

时间:2017-06-30 15:43:43

标签: javascript arrays node.js regex string

我目前的意见......

var str =“3。1203 Copra 4. 1204亚麻籽,无论是否破碎.5。1205强奸或菜子种子,无论是否破碎.6。1206向日葵种子,无论是否破碎。”

我需要输出的是

[1203 Copra,204 Linseed,无论是否破碎。,1205强奸或菜子种子,无论是否破碎。,等等)

就像我需要拆分字符串一样简单,我找到带点的数字。

4 个答案:

答案 0 :(得分:3)

您可以使用String#match处理它。

const str = "1.one 2. two 3.three",
      res = str.match(/(?!\d\.\s?)\w+/g);
      
      console.log(res);

答案 1 :(得分:2)

您可以使用只带一组字母的正则表达式。

shape-rendering="crispEdges"

答案 2 :(得分:1)

要匹配字符串中编号列表的项并返回数组,请尝试以下操作:

var x = "1. Buy bananas 2. Buy a tent  3. Book flight to jungle  4. Eat banana in jungle and 5. come up with better sample for regex"
var result = x.match(/(?!\d\.\s)(\w+\D+)/gm);
console.log(result)

答案 3 :(得分:0)

对于您当前的字符串 - 只需匹配包含字母的字词:

var str = "1.one 2. two 3.three",
    result = str.match(/[^0-9. ]+/g);
    
console.log(result);