How to Specify Single Quote in Regular Expression

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

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

4 Answers

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

3

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.

2
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

/^[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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Sophia Al-Mansoor

Sophia Al-Mansoor

Global Business & E-Commerce Reporter

Sophia analyzes international trade, startup ecosystems, retail transformation, and supply chain logistics for modern digital publications.

Share this article
Twitter Facebook Pinterest