今天有一个问题,引起了我的兴趣(并随后被删除),用户希望将以下字符串与正则表达式分开
'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"]
单元测试here
当然可以这样做,但是怎么做?
答案 0 :(得分:1)
怎么样
的Javascript
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"]
上
答案 1 :(得分:0)
str.match(/\b(?!The\b)[a-z]+\b/gi)
答案 2 :(得分:0)
您可以使用此模式:
str.match(/\b[a-z]+\b(?!\s1\.)/gi)