正则表达式用于拆分成句子,而忽略了十进制数字作为拆分的一部分?

时间:2018-09-06 16:31:49

标签: javascript regex

在javascript中,我有兴趣将文本主体分割为数组句子,在该句子中它将忽略十进制数字(理想情况下为网站)。我已经找到了如何对句子进行操作-例如str.split(/[\.\!]+\s*|\n+\s*/)-但不确定如何添加额外的位以忽略拆分中的十进制数字

例如,如果

str = "Hello there, the ice cream is $2.00.Toppings are extra."

会导致

["Hello there, the ice cream is $2.00", "Toppings are extra"]

这可能吗?

谢谢!

1 个答案:

答案 0 :(得分:1)

str = "Hello there, the ice cream is $2.00.Toppings are extra.";    
str.split(/[\.\!]+(?!\d)\s*|\n+\s*/); //[ 'Hello there, the ice cream is $2.00', 'Toppings are extra',]
  • (?!\d)零宽度负向超前匹配d匹配。

如果先行匹配,则不匹配正则表达式,并且不分割字符串。