【发布时间】:2014-12-29 22:38:23
【问题描述】:
我需要一个位于两个字符串之间的字符串数组,但是当我使用 str.match 时,结果不是我所期望的:
var text = "first second1 third\nfirst second2 third\nfirst second3 third";
var middles = text.match(/first (.*?) third/g);
console.log(middles); //this should be ["second1", "second2", "second3"]
结果:
["first second1 third", "first second2 third", "first second3 third"]
有什么我可以尝试只获取每次出现的中间字符串吗?
【问题讨论】:
-
如果 Javascript 支持lookbehind,这会容易得多。然后你可以做
/(?<=first ).*?(?= third)/g -
/(?=first )(.*)( ?=third)/g 这行得通,但仍然包括 first
标签: javascript regex string substring match