简体   繁体   English

正则表达式以查找复杂的标记

[英]Regular Expression to find complex markers

I want to use JavaScript's regular expression something like this 我想使用JavaScript的正则表达式

/marker\d+"?(\w+)"?\s/gi

In a string like this: 在这样的字符串中:

IDoHaveMarker1"apple" IDoAlsoHaveAMarker352pear LastPointMakingmarker3134"foo"

And I want it to return an array like this: 我希望它返回这样的数组:

[ "apple", "pear", "foo" ]

The quotes are to make clear they are strings. 引号是为了明确它们是字符串。 They shouldn't be in the result. 他们不应该出现在结果中。

If you are asking about how to actually use the regex: 如果您询问如何实际使用正则表达式:

To get all captures of multiple (global) matches you have to use a loop and exec in JavaScript: 要获取多个(全局)匹配的所有捕获,您必须在JavaScript中使用循环和exec

var regex = /marker\d+"?(\w+)/gi;
var result = [];
var match;
while (match = regex.exec(input)) {
    result.push(match[1]);
}

(Note that you can omit the trailing "?\\s? if you are only interested in the capture, since they are optional anyway, so they don't affect the matched result.) (请注意,如果只对捕获感兴趣,可以省略结尾的"?\\s? ,因为它们始终是可选的,因此它们不会影响匹配的结果。)

And no, g will not allow you to do all of that in one call. 不, g不允许您一次调用所有这些操作。 If you had omitted g then exec would return the same match every time. 如果省略了gexec每次都会返回相同的匹配项。

As Blender mentioned, if you want to rule out things like Marker13"something Marker14bar (unmatched " ) you need to use another capturing group and a backreference. 正如Blender提到的,如果要排除诸如Marker13"something Marker14bar (不匹配的" ),则需要使用另一个捕获组和一个反向引用。 Note that this will push your desired capture to index 2 : 请注意,这会将您想要的捕获推送到索引2

var regex = /marker\d+("?)(\w+)\1/gi;
var result = [];
var match;
while (match = regex.exec(input)) {
    result.push(match[2]);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM