Skip to content
Advertisement

How do I include – and ‘ in this regular expressions?

I have a this regular expression below for some input name fields. How do I include an apostrophe and a hyphen in this?

InputField("tFName", /^[a-zA-Z-- ]+$/);

Advertisement

Answer

Hyphen is already included (twice), you can add the apostrophe by just editing it into the character class:

/^[a-zA-Z-- ']+$/

You can rewrite it to look like this, so that there’s no need to escape the hyphen and it’s only included once:

/^[a-zA-Z '-]+$/

Example: http://jsfiddle.net/a4vGA/

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement