代码之家  ›  专栏  ›  技术社区  ›  user655489

正则表达式用于拆分成句子,忽略小数作为拆分的一部分?

  •  1
  • user655489  · 技术社区  · 6 年前

    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 回复  |  直到 6 年前
        1
  •  2
  •   TheMaster    6 年前
    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

    如果lookahead匹配,则regex不匹配,并且字符串不被拆分。