正则表达式只验证A-Z、A-Z、0-9、空间、周期、连字符-感叹号!问号?引用“

时间:2022-02-25 22:25:50

I want to use regular expression which validate the text which user enter.

我想使用正则表达式来验证用户输入的文本。

/^[a-zA-Z0-9 ]+$/

By above line, we can allow only Alphabetic, Numbers and Space.

在上面一行,我们只允许字母、数字和空格。

What will be regular expression to allow:

什么是正则表达式允许:

alphabetic,
numbers,
space,
period .
hyphen -
exclamation mark !
question mark ?
quotes "

字母,数字,空格,句号。连字符-感叹号!问号?引用“

Except above characters user can not enter other characters.

除上述字符外,用户不能输入其他字符。

Thanks, naveenos

谢谢,naveenos

3 个解决方案

#1


5  

You just need to include this extra symbols in the character class you have in your regex.

你只需要在你的正则表达式中包含这个额外的符号。

You can use this regex:

你可以使用这个regex:

/^[a-zA-Z0-9 "!?.-]+$/

#2


3  

You're almost there. Try this:

你差不多了。试试这个:

/^[a-zA-Z0-9 .!?"-]+$/

Note that the position of the - character is important. If it appears between two characters (e.g. a-z) it represents a character range. If it appears in at the beginning or end of the character class (or if it's escaped) it represents a literal hyphen character.

注意,角色的位置很重要。如果它出现在两个字符之间(例如a-z),它表示一个字符范围。如果它出现在字符类的开头或结尾(或者它被转义了),它表示一个字连字符。

#3


-1  

try this pattern

试试这个模式

<?php /^[a-zA-Z0-9 \s\.\!\?\"\-]+$/ ?>

#1


5  

You just need to include this extra symbols in the character class you have in your regex.

你只需要在你的正则表达式中包含这个额外的符号。

You can use this regex:

你可以使用这个regex:

/^[a-zA-Z0-9 "!?.-]+$/

#2


3  

You're almost there. Try this:

你差不多了。试试这个:

/^[a-zA-Z0-9 .!?"-]+$/

Note that the position of the - character is important. If it appears between two characters (e.g. a-z) it represents a character range. If it appears in at the beginning or end of the character class (or if it's escaped) it represents a literal hyphen character.

注意,角色的位置很重要。如果它出现在两个字符之间(例如a-z),它表示一个字符范围。如果它出现在字符类的开头或结尾(或者它被转义了),它表示一个字连字符。

#3


-1  

try this pattern

试试这个模式

<?php /^[a-zA-Z0-9 \s\.\!\?\"\-]+$/ ?>