简体   繁体   中英

Optional Characters, Numbers, Symbols regex

The regex task that I am trying to achieve is pretty simple. A string can contain one of the following:

  • alphabet plus numbers
  • alphabet plus numbers plus symbols
  • alphabet plus symbols

I have been able to achieve the first two points but I am struggling to get the last one.

/^(?=.*[a-z])(?=.*[0-9]).{7,}$/

Strings that should match are as followed:

  • test123
  • test123@#][
  • test@#;[-=

Please note that for the last scenario when I say alphabet plus symbols . I'd like it to match any symbols. I did alternatively try:

/(.*).{7}/

And this did not work. Reason being because that means an individual could enter just letters. So in order for the regex to satisfy it needs to be one of the following above as stated.

根据我的解释,这可能有效:

/^[a-zA-Z]+[^a-zA-Z]+$/

I sense that by "plus" you mean "then", so this should do it:

^(?=.{7,})[a-zA-Z]+(?!$)[0-9]*[^a-zA-Z0-9]*$

This regex requires that if numbers and symbols are present, that number precede symbols.

Here "symbol" is defined as any character not a letter or number, you may want to instead list the characters that are "symbols", eg [@%#()*]

A negative look ahead is used to require that at least some characters must follow the letters.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM