Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
592 views
in Technique[技术] by (71.8m points)

javascript - Regular expression for no more than two repeated letters/digits

I have a requirement to handle a regular expression for no more than two of the same letters/digits in an XSL file.

  • no space
  • does not support special chars
  • support (a-z,A-Z,0-9)
  • require one of a-z
  • require one of 0-9
  • no more than 2 same letter/digits (i.e., BBB will fail, BB is accepted)

What I have so far

(?:[^a-zA-Z0-9]{1,2})
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This regex will do it: ^(?!.*([A-Za-z0-9])1{2})(?=.*[a-z])(?=.*d)[A-Za-z0-9]+$

Here's the breakdown:

(?!.*([A-Za-z0-9])1{2}) makes sure that none of the chars repeat more than twice in a row.

(?=.*[a-z]) requires at least one lowercase letter

(?=.*d) requires at least one digit

[A-Za-z0-9]+ allows only letters and digits

EDIT : removed an extraneous .* from the negative lookahead


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...