正则表达式将字符串拆分为数组,其中数字后跟空格,但跳过后面没有空格的数字

Regex to split string into array where numeral exists followed by space, but skip over numerals that don't have a space after them

提问人:Niwa 提问时间:4/26/2023 更新时间:4/26/2023 访问量:32

问:

考虑下面的字符串;

1 The dog is big 2 The dog is sitting on the sofa 3 The dog was born on the 4th of July 4 The dog is named Henry

我想在数字后跟空格的地方分解它,收集数字和它后面的所有内容,直到下一个数字后跟一个空格。我正在使用有效的正则表达式,除非句子中间有一个数字,而不是后面跟着一个空格(在本例中为 4th)。所以,我想得到这个:\s*\d+\D+

["1 The dog is big","2 The dog is sitting on the sofa","3 The dog was born on the 4th of July","4 The dog is named Henry"]

但相反,我得到了这个:

["1 The dog is big","2 The dog is sitting on the sofa","3 The dog was born on the","4th of July","4 The dog is named Henry"]

我将如何实现这一目标,请记住,我希望避免前瞻条款,因为我正在使用 safari,而它们不适用于 safari?

jQuery 数组正 则表达式 拆分

评论


答:

1赞 Tim Biegeleisen 4/26/2023 #1

您可以使用以下方法:match()

var input = "1 The dog is big 2 The dog is sitting on the sofa 3 The dog was born on the 4th of July 4 The dog is named Henry";
var parts = input.match(/\b\d+(?: \w+)+?(?=\s+\d+\b|$)/gi);
console.log(parts);

此处使用的正则表达式模式将每个编号项匹配为:

  • \b\d+匹配前导数字
  • (?: \w+)+?匹配一个或多个单词,直到
  • (?=\s+\d+\b|$)向前看,看到下一个最接近的数字或输入的末尾

评论

0赞 Niwa 4/26/2023
谢谢!我还想补充一点,我需要为其他语言的文本包含它,因此 \D 和 \w 函数不适用于代码的那部分。此外,某些输入文本具有换行符,对于所有字符减去行终止符,这些换行符不适用于 .+。使用您代码中的想法,我想出了这个\s*\d+\s+[\s\S]*?(?=\s+\d+\s+|$),它也可以捕获非英语字符。