tags:

views:

727

answers:

1

I have had a few cracks at this but can't seem to get it right. Anyone have a regex to allow alphanumerics and -_",' as well as white spaces.

Thx

+1  A: 

Try this one:

/^[A-Za-z0-9-_",'\s]+$/
Gumbo
You'd better explain the `0-9-` trick :)
Andomar
try escaping the single and double quotes *inside* the regex: `/^[A-Za-z0-9-_\",\'\s]+$/`
pavium
Yep, beaut. Thanks
Is the `-` between the `9` and `_` intended to match a literal `-`? If so, putting it at the beginning or end of the class might make the expression more suitable to other languages.
Tim
@jason: You need to escape it properly: `"/^[A-Za-z0-9-_\",'\\s]+\$/"`.
Gumbo
@Tim: What language does not recognize a literal `-` if it’s not at the start or the end of a character class?
Gumbo
@Gumbo, true, probably in all PCRE flavours, you needn't escape the hyphen if it's preceded by a character range or shorthand character class. However, IMO it's (1) easier to understand when placed at the start or end of the character class and (2) if the requirements change and the `0-9` part is removed, bugs can creep in the regex if all of a sudden the `-` gets treated as a range operator.
Bart Kiers