简体   繁体   English

正则表达式以匹配这种重复模式?

[英]regex to match this repeating pattern?

I was able to match the string [ORG] someText with this regex: /^\\[(ORG|PER|LOC)]\\s[^\\W_]+$/ 我可以用此正则表达式匹配字符串[ORG] someText/^\\[(ORG|PER|LOC)]\\s[^\\W_]+$/

 var selectedText = "[ORG] dd"; if (selectedText.match(/^\\[(ORG|PER|LOC)]\\s[^\\W_]+$/)) { console.log("working"); } 

The input text could have anything in the tag followed by any word. 输入文本可以在标签中包含任何内容,后跟任何单词。


Now I have the text as: [ORG] Lorem [ORG] ipsum (ending with a space) 现在,我的文字为: [ORG] Lorem [ORG] ipsum (以空格结尾)

I tried to match this by grouping the pattern and repeating it with a + (one or more occurances). 我试图通过将模式分组并用+ (一个或多个事件)重复它来匹配它。

This way: /^(\\[(ORG|PER|LOC)]\\s[^\\W_]\\s)+$/ 这样: /^(\\[(ORG|PER|LOC)]\\s[^\\W_]\\s)+$/

However it doesnt match. 但是,它不匹配。

Basically, it should match: 基本上,它应该匹配:

[tag] sometext

[tag] sometext [tag2] someOtherText // ending with or without a space

So, in general, it needs to match a pattern of a tag followed by a space and a word . 因此,通常,它需要匹配a tag followed by a space and a word的模式, a tag followed by a space and a word

What it shouldnt match: 它不应该匹配的是:

[tag] sometext someMoreText

[tag] sometext someMoreText [tag9]

[tag] [tag9] sometext someMoreText 

It should be: 它应该是:

/^(\[(ORG|PER|LOC)]\s[^\W_]+(?:\s|$))+$/

... that is, adding an alternation between a whitespace and the end-of-line boundary (for the last pattern in the string doesn't end with a whitespace). ...也就是说,在空格和行尾边界之间添加一个替代(对于字符串中的最后一个模式,它并不以空格结尾)。

Demo . 演示 Also note that if you only need to check whether or not a string matches that pattern, String#match method is actually an overkill; 还要注意,如果只需要检查字符串是否与该模式匹配,则String#match方法实际上是一个替代品; instead you should RegExp#text : 相反,您应该RegExp#text

var tagsPattern = /^(\[(ORG|PER|LOC)]\s[^\W_]+(\s|$))+$/;
if (tagsPattern.test(str)) {
  // matches 
}

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

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