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-\-\ ]+$/);
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 \- .
$ means "Match the end of the string" (the position after the last character in the string).
to combine two expressions or more, put every expression in brackets, and use: *? This are the signs to combine, in order of relevance: ?
[] denotes a character class. () denotes a capturing group. (a-z0-9) -- Explicit capture of a-z0-9 . No ranges.
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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With