将字符串拆分为一个数组,其中正则表达式不包括单词



今天提出了一个问题,引起了我的兴趣(随后被删除),用户想用正则表达式拆分以下字符串

'The 1. cat 2. sat 3. on 4. the 5. mat';

进入此数组

["cat","sat","on","the","mat"]

这个表达有答案

str.match(/[a-z]+/gi);

哪个当然返回

["The","cat","sat","on","the","mat"]

我最接近答案的是

str.match(/[^The][a-z]+/gi);

返回

[" cat"," sat"," on"," the"," mat"]

此处测试的单元

这当然可以做到,但如何做到呢?

怎么样

爪哇语

var str = 'The 1. cat 2. sat 3. on 4. the 5. mat',
    arr1 = str.match(/[a-z]+/gi),
    arr2 = str.match(/b[a-z]+/g);
console.log(arr1);
console.log(arr2);

输出

["The", "cat", "sat", "on", "the", "mat"] 
["cat", "sat", "on", "the", "mat"] 

在jsFiddle上

str.match(/b(?!Theb)[a-z]+b/gi)
您可以使用

此模式:

str.match(/b[a-z]+b(?!s1.)/gi)

最新更新