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]).*\1/;
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,output - false(有效)
abcda,输出 - 真(有效)
aabcdaa,输出 - 真(有效)
aeqwae,输出 - 真(无效)
ouqweru,输出 - true(无效)
1 个解决方案
#1
13
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 a
is 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不同。好吧,正则表达式只是匹配前一个字符(在e之前),这是一个。因此,匹配有效。所以,你得到这个:
[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]).*\1$/
By adding ^
, you tell it that the start of the match must be the start of the string and by adding $
you tell it that the end of the match must be the end of the string. This way, if there's a match, the whole string must be matched, meaning that aeqwae
will 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)标志。全局标志阻止正则表达式在第一次匹配后返回。多行标志使^和$匹配行的开头和结尾(不是字符串)。在使用您的输入进行测试时,我使用了它们。
#1
13
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 a
is 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不同。好吧,正则表达式只是匹配前一个字符(在e之前),这是一个。因此,匹配有效。所以,你得到这个:
[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]).*\1$/
By adding ^
, you tell it that the start of the match must be the start of the string and by adding $
you tell it that the end of the match must be the end of the string. This way, if there's a match, the whole string must be matched, meaning that aeqwae
will 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)标志。全局标志阻止正则表达式在第一次匹配后返回。多行标志使^和$匹配行的开头和结尾(不是字符串)。在使用您的输入进行测试时,我使用了它们。