最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Regular expression for y,yes, Y, YES ,1 - Stack Overflow

programmeradmin4浏览0评论

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.

Share Improve this question edited Aug 24, 2015 at 2:11 Alan Moore 75.3k13 gold badges107 silver badges161 bronze badges asked Aug 17, 2015 at 5:24 Hitesh KumarHitesh Kumar 3,7089 gold badges48 silver badges73 bronze badges 3
  • 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
Add a ment  | 

1 Answer 1

Reset to default 9

You 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)

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论