modify a regex so that only [a
This question already has an answer here:
截至2018年,Javascript最终支持lookbehind断言,所以一旦它实现,以下应该在最新的浏览器中工作:
test = "i am sam";
console.log(test.match(/(?<=i'm |i am )[a-zA-Z]+/))
You could capture your words using a capturing group ([a-zA-Z]+)
:
I ?['a]m ([a-zA-Z]+)
This would match
I # Match I ? # Match an optional white space ['a]m # Match ' or a followed by an m and a whitespace ( # Capture in a group [a-zA-Z]+ # Match lower or uppercase character one or more times ) # Close capturing group
Your words are in group 1.
var pattern = /I ?['a]m ([a-zA-Z]+)/;
var strings = [
"I am good at this",
"I'm sam"
];
for (var i = 0; i < strings.length; i++) {
console.log(strings[i].match(pattern)[1]);
}
链接地址: http://www.djcxy.com/p/76812.html
下一篇: 修改一个正则表达式,只有[a