This will filter the characters for (1):
/[^a-zA-Z0-9\-_ ]/g
/ are regular expression delimiters in JavaScript. [] will separate the character class, and ^ inside the character class means "combine all non characters contained within this class." You can then specify individual characters within the class (for example, "-" and "_") or specify a range (for example, "az").
g outside the delimiters is used as a flag for "global search", which means that the regular expression matches more than once, otherwise it stops at the first match and then stops the search. \ used as an escape character, which I use to remove a hyphen in a character class, because it is used as a metacharacter inside character classes to indicate character ranges.
I'm not sure if this is necessary because I did not find anything in the JavaScript, PHP, and Mozilla JavaScript documents that say hyphens should be escaped this way, and my tests in Firefox 5 and Chrome 12 show that it works in any case.
Learn more about JavaScript regex in Mozilla docs and check your regex at http://regexpal.com/ .
user456814
source share