Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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-\-\ ]+$/);
like image 640
Amen Ra Avatar asked Sep 07 '10 15:09

Amen Ra


People also ask

How do you include in regex?

Most of the special regex characters lose their meaning inside bracket list, and can be used as they are; except ^ , - , ] or \ . To include a ] , place it first in the list, or use escape \] . To include a ^ , place it anywhere but first, or use escape \^ . To include a - place it last, or use escape \- .

What does '$' mean in regex?

$ means "Match the end of the string" (the position after the last character in the string).

How do you combine two regular expressions?

to combine two expressions or more, put every expression in brackets, and use: *? This are the signs to combine, in order of relevance: ?

What's the difference between () and [] in regular expression?

[] denotes a character class. () denotes a capturing group. (a-z0-9) -- Explicit capture of a-z0-9 . No ranges.


1 Answers

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/

like image 168
Andy E Avatar answered Nov 14 '22 22:11

Andy E