admin管理员组文章数量:1026989
I need to write a regex for validating a string. The regular expression should pass the string if it contains any of the following: y
, Y
, yes
, YES
, 1
. The letters can be in any case. I am new to regular expression and JavaScript.
I need to write a regex for validating a string. The regular expression should pass the string if it contains any of the following: y
, Y
, yes
, YES
, 1
. The letters can be in any case. I am new to regular expression and JavaScript.
- 2 Have you tried anything? Looks simple – Tushar Commented Aug 17, 2015 at 5:25
- 'hello y here is yes i am Y you are Yes'.match(/yes|y|1/gi) remove g if you know that only one of them exists – Harpreet Singh Commented Aug 17, 2015 at 5:27
- contains any of the following: do you mean "contains", or do you mean "exactly equal to"? – user663031 Commented Aug 24, 2015 at 2:38
1 Answer
Reset to default 9You need to add an optional group as well as a case-insensitive i
modifier.
/y(?:es)?|1/i.test(str)
or
/[1y](?:es)?/i.test(str)
or
/[y1]/i.test(str)
For doing exact match.
/^(?:y(?:es)?|1)$/i.test(str)
I need to write a regex for validating a string. The regular expression should pass the string if it contains any of the following: y
, Y
, yes
, YES
, 1
. The letters can be in any case. I am new to regular expression and JavaScript.
I need to write a regex for validating a string. The regular expression should pass the string if it contains any of the following: y
, Y
, yes
, YES
, 1
. The letters can be in any case. I am new to regular expression and JavaScript.
- 2 Have you tried anything? Looks simple – Tushar Commented Aug 17, 2015 at 5:25
- 'hello y here is yes i am Y you are Yes'.match(/yes|y|1/gi) remove g if you know that only one of them exists – Harpreet Singh Commented Aug 17, 2015 at 5:27
- contains any of the following: do you mean "contains", or do you mean "exactly equal to"? – user663031 Commented Aug 24, 2015 at 2:38
1 Answer
Reset to default 9You need to add an optional group as well as a case-insensitive i
modifier.
/y(?:es)?|1/i.test(str)
or
/[1y](?:es)?/i.test(str)
or
/[y1]/i.test(str)
For doing exact match.
/^(?:y(?:es)?|1)$/i.test(str)
本文标签: javascriptRegular expression for yyesY1Stack Overflow
版权声明:本文标题:javascript - Regular expression for y,yes, Y, YES ,1 - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1744624292a2107433.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论