开发者

How to Specify single quote in regular expression

开发者 https://www.devze.com 2023-04-04 21:18 出处:网络
Here is my regular expression . I want to include single quotes (\') within the 开发者_JS百科characters such as (O\'Neal Nickel). Here is my regular expression allowing letters and spaces and full sto

Here is my regular expression . I want to include single quotes (') within the 开发者_JS百科characters such as (O'Neal Nickel). Here is my regular expression allowing letters and spaces and full stops(.) and (-) hyphens

 /^[A-Za-z\/\s\.-]+$/; 


/^[A-Za-z\/\s\.'-]+$/; Or did I get your question wrong?


Try this:

/^[A-Za-z\/\s\.'\-]+$/;

You do not need to backslash/escape the single quote. Doing so will cause Safari to disallow single quote marks instead of allowing them.


/^[A-Za-z\/\s\.'-]+$/

@Urasquirrel - you asked for detailed explanation and I know that whenever I see questions like this, I want the same thing!!

^ - beginning of the string 
[ - allowed characters are contained in the square brackets
A-Z - uppercase A to Z
a-z - lowercase a to z
\/ - escaped forward slash
\s - whitespace
\. - escaped period
' - single quote
- - a dash
] - end of square brackets
+ - ONE of more of the allowed characters contained in the square brackets
$ - end of string

https://regex101.com/ is a great place to test different RE patterns with different strings and different languages. there are other sites that do similar


var testName=/^[A-Za-z\/\s\.'-]+$/;
var name= $("#name").val();
if(testName.test(name) == false || name == "")
{}

This is a code I used to check name

0

精彩评论

暂无评论...
验证码 换一张
取 消