Javascript 使用正则表达式匹配字符串的相同开始和结束字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46387760/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Match the same start and end character of a string with Regex
提问by Nesh
I'm trying to match the start and end character of a string to be the same vowel. My regex is working in most scenarios, but failing in others:
我试图将字符串的开始和结束字符匹配为相同的元音。我的正则表达式适用于大多数情况,但在其他情况下失败:
var re = /([aeiou]).*/;
re.test(str);
Sample input:
样本输入:
abcde, output - false (Valid)abcda, output - true (Valid)aabcdaa, output - true (Valid)aeqwae, output - true (Not valid)ouqweru, output - true (Not valid)
abcde, 输出 - 假(有效)abcda, 输出 - 真(有效)aabcdaa, 输出 - 真(有效)aeqwae, 输出 - 真(无效)ouqweru, 输出 - 真(无效)
回答by dodov
You need to add anchors to your string.
您需要在字符串中添加锚点。
When you have, for example:
当你有,例如:
aeqwae
You say the output is true, but it's not valid because ais not the same as e. Well, regex simply matches the previous character (before e), which is a. Thus, the match is valid. So, you get this:
您说输出为真,但它无效,因为a与e. 好吧,正则表达式只是匹配前一个字符(before e),即a. 因此,匹配是有效的。所以,你得到这个:
[aeqwa]e
The string enclosed in the brackets is the actual match and why it returns true.
括号中的字符串是实际匹配项以及它返回的原因true。
If you change your regex to this:
如果您将正则表达式更改为:
/^([aeiou]).*$/
By adding ^, you tell it that the start of the match mustbe the start of the string and by adding $you tell it that the end of the match mustbe the end of the string. This way, if there's a match, the whole string must be matched, meaning that aeqwaewill no longer get matched.
通过添加^,你告诉它匹配的开始必须是字符串的开始,通过添加$你告诉它匹配的结束必须是字符串的结束。这样,如果有匹配项,则必须匹配整个字符串,这意味着aeqwae将不再匹配。
A great tool for testing regex is Regex101. Give it a try!
测试正则表达式的一个很好的工具是Regex101。试一试!
Note:Depending on your input, you might need to set the global (g) or multi-line (m) flag. The global flag prevents regex from returning after the first match. The multi-line flag makes ^and $match the start and end of the line(not the string). I used both of them when testing with your input.
注意:根据您的输入,您可能需要设置全局 (g) 或多行 (m) 标志。全局标志可防止正则表达式在第一次匹配后返回。多线标志使^与$匹配的开始和结束行(不是字符串)。在使用您的输入进行测试时,我同时使用了它们。
回答by Pablo
Just a different version of @Hristiyan Dodov answer that I have written for fun.
只是一个不同版本的@Hristiyan Dodov 回答,我是为了好玩而写的。
regex = /^(a|e|i|o|u).*$/
const strings = ['abcde', 'abcda', 'aabcdaa', 'aeqwae', 'ouqweru']
strings.forEach((e)=>{
const result = regex.test(e)
console.log(e, result)
})
回答by beingGeek
Correct answer is already mentioned above, just for some more clarification:
上面已经提到了正确答案,只是为了进一步澄清:
regEx= /^([a,e,i,o,u])(.*)$/
Here, \1is the backreference to match the same text again, you can reuse the same backreference more than once. Most regex flavors support up to 99 capturing groups and double-digit backreferences. So \99 is a valid backreference if your regex has 99 capturing groups.visit_for_detail
这里,\1是再次匹配相同文本的反向引用,您可以多次重复使用相同的反向引用。大多数正则表达式支持多达 99 个捕获组和两位数的反向引用。所以 \99 是一个有效的反向引用,如果你的正则表达式有 99 个捕获组。访问详细信息
回答by geekme
/^([aeiou])[a-z]$/
just a bit of improvement, to catch alphabet letters.
只是一点点改进,以捕捉字母。

