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

javascript - Regular Expression to handle multiple conditions - Stack Overflow

programmeradmin3浏览0评论

Can anyone help me with a javascript regular expression that satisfies the following conditions (it is to validate input in textarea):

  1. there should be minimum 3 characters
  2. the first character is always /
  3. the entire input string shouldnot match the exact string '/abc' and '/xyz' but it can be anyother like /abce or /abct etc...

Ex: the input such as /xyzw, /ab, /abcd, /asdad are accepted and such as /a, /abc etc. are not accepted

Can anyone help me with a javascript regular expression that satisfies the following conditions (it is to validate input in textarea):

  1. there should be minimum 3 characters
  2. the first character is always /
  3. the entire input string shouldnot match the exact string '/abc' and '/xyz' but it can be anyother like /abce or /abct etc...

Ex: the input such as /xyzw, /ab, /abcd, /asdad are accepted and such as /a, /abc etc. are not accepted

Share Improve this question edited Feb 28, 2012 at 11:37 Pradeep asked Feb 20, 2012 at 14:17 PradeepPradeep 991 gold badge4 silver badges15 bronze badges 2
  • /^\/(?!abc$|xyz$)[\S\s]{2,}/ – Rob W Commented Feb 20, 2012 at 14:19
  • 1 Your condition 3 needs clarification. It can't match /xyz but then later that is ok? – YXD Commented Feb 20, 2012 at 14:20
Add a ment  | 

1 Answer 1

Reset to default 7
/^\/(?!abc$|xyz$)[\S\s]{2,}/

Meaning:

/             
^             Start of string
\/            "/"
(?!abc$|xyz$) Not followed by only abc or xyz ($ = end of string)
[\S\s]{2,}    At least two characters.
/
发布评论

评论列表(0)

  1. 暂无评论