在本文中:
my-Word: Value-1
othertext
my-Word: Value-2
othertext
my-Word: Value-3
...我需要匹配所有包含:([A-Za-z0-9-]+)的字符串
它们只在字符串:my-Word:之后,但不包括:my-Word:
所以我只需要匹配:Value-1、Value-2、Value-3等。
我该怎么做呢?
发布于 2014-10-17 04:33:10
你可以使用正向回溯:
(?<=my-Word:\s*)([A-Za-z0-9-]+)发布于 2014-10-17 04:31:49
使用捕获组来捕获在to my-Word:之后出现的字母数字字符
> var s = "my-Word: Value-1\nothertext\nmy-Word: Value-2\nothertext\nmy-Word: Value-3"
undefined
> var re = /my-Word:\s*([A-Za-z0-9-]+)/gm;
undefined
> var m;
undefined
> while ((m = re.exec(s)) != null) {
... console.log(m[1]);
... }
Value-1
Value-2
Value-3发布于 2014-10-17 04:37:16
您必须使用后置正则表达式,例如:
.(?<=my-Word: [A-Za-z0-9-])[A-Za-z0-9-]+但不幸的是,不支持后视,所以你可以使用前视javascript正则表达式来代替。为此,您需要首先反转原始字符串,最后反转匹配的部分:
[A-Za-z0-9-]+(?= :droW-ym)DEMO
https://stackoverflow.com/questions/26413154
复制相似问题