Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Specify single quote in regular expression

Tags:

regex

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\.-]+$/; 
like image 279
Someone Avatar asked Sep 12 '11 18:09

Someone


People also ask

How do you include a quote in regex?

Try putting a backslash ( \ ) followed by " .

What does '$' mean in regex?

$ means "Match the end of the string" (the position after the last character in the string). Both are called anchors and ensure that the entire string is matched instead of just a substring.

How do you match a single character with a regular expression?

Match any specific character in a setUse square brackets [] to match any characters in a set. Use \w to match any single alphanumeric character: 0-9 , a-z , A-Z , and _ (underscore). Use \d to match any single digit. Use \s to match any single whitespace character.


2 Answers

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

like image 188
rdmueller Avatar answered Oct 13 '22 05:10

rdmueller


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.

like image 40
kasper Taeymans Avatar answered Oct 13 '22 05:10

kasper Taeymans