简体   繁体   English

正则表达式只允许选择字母字符

[英]Regex to allow only select characters of alphabet

Can someone define a regex set that would only allow two letters "E" and "P". 有人可以定义仅允许两个字母“ E”和“ P”的正则表达式集吗?

I have a web app where certain text areas need to allow entry of just those two characters. 我有一个Web应用程序,其中某些文本区域需要允许仅输入这两个字符。

Thank you. 谢谢。

/^[ep]+$/

That's it. 而已。

  • ^ means "Start of string" ^表示“字符串开头”
  • [ep] is our characters "e" and "p" [ep]是我们的字符“ e”和“ p”
  • + means "repeated 1 or more times" +表示“重复1次或多次”
  • $ 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: 如果还希望接受E和P,则可以添加i修饰符以区分大小写:

/^[ep]+$/i

To use in javascript: 在javascript中使用:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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