[英]JavaScript Regular Expression to repeat element match in string
Yes I am a newbie, I have looked online and cannot seem to find the answer to the following, I know it must be simple.是的,我是新手,我在网上查过,似乎找不到以下问题的答案,我知道它一定很简单。
I have a simple string and need to match spaced Capitals eg TG D......repeater, Secondly I need to match capitals with a dot between them and no space eg TGD.......repeater我有一个简单的字符串,需要匹配间隔大写,例如 TG D......repeater,其次我需要匹配大写,它们之间有一个点并且没有空格,例如 TGD.......repeater
I have the current string = str.match(/ [AZ] [AZ] | [AZ].[AZ]/g)我有当前字符串 = str.match(/ [AZ] [AZ] | [AZ].[AZ]/g)
but this will only match the first two eg TG I Need it to match wherever it finds the following pattern eg TGDEF L...repeater as a one match但这只会匹配前两个,例如TG I Need it to match where it finds the following pattern eg TGDEF L...repeater as a one match
Likewise It will only match the TG but nothing after eg TG I Need to match T.GDLT repeater (may end with a dot and may not)同样,它只会匹配 TG,但在TG之后不匹配,我需要匹配T.GDLT中继器(可能以点结尾,也可能不匹配)
any help will be appreciated.任何帮助将不胜感激。
You might use a capturing group matching either a dot or a space in a character class ([. ])
您可以使用匹配字符 class
([. ])
中的点或空格的捕获组
Match the first 2 capitals and capture the space or dot in group 1. Then optionally repeat what is captured using a back reference followed by a captital AZ.匹配前 2 个大写字母并捕获第 1 组中的空格或点。然后可选择使用反向引用和大写字母 AZ 重复捕获的内容。
\b[A-Z]([. ])[A-Z](?:\1[A-Z])*\b
\b
Word boundary \b
字边界[AZ]([. ])[AZ]
Match AZ, capture in group 1 matching either space or .
[AZ]([. ])[AZ]
匹配 AZ,在第 1 组中捕获,匹配空格或.
(?:\1[AZ])*
Repeat 0+ times matching what it captured in group 1 followed by AZ (?:\1[AZ])*
重复 0+ 次,匹配它在第 1 组中捕获的内容,然后是 AZ\b
Word boundary \b
字边界
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.