简体   繁体   中英

Regex to allow only select characters of alphabet

Can someone define a regex set that would only allow two letters "E" and "P".

I have a web app where certain text areas need to allow entry of just those two characters.

Thank you.

/^[ep]+$/

That's it.

  • ^ means "Start of string"
  • [ep] is our characters "e" and "p"
  • + means "repeated 1 or more times"
  • $ means "end of string"

In case you also want to accept E and P as well, you can add an i modifier for case-insensitivity:

/^[ep]+$/i

To use in javascript:

var testingRegex = /^[ep]+$/i;

if (testingRegex.match(myString)) {
    //Yes, it matches!
} else {
    //Error!
}

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