I've searched and searched and tried many different ways, but I can't seem to figure this out. I'm looking for a way to only allow alphanumeric characters, then only one space, then alphanumeric characters. I'm sure it's easy, but I don't know it.
Examples of what I want:
First Last Allowed First La-st Not Allowed FirstLast Not Allowed First Last Not Allowed First La'st Not allowed
I'd then like to remove the invalid characters from the string.
Please let me know if you need more information. Thanks a lot!
Alphanumeric characters by definition only comprise the letters A to Z and the digits 0 to 9. Spaces and underscores are usually considered punctuation characters, so no, they shouldn't be allowed.
The most common forms of whitespace you will use with regular expressions are the space (␣), the tab (\t), the new line (\n) and the carriage return (\r) (useful in Windows environments), and these special characters match each of their respective whitespaces.
Use square brackets [] to match any characters in a set. Use \w to match any single alphanumeric character: 0-9 , a-z , A-Z , and _ (underscore). Use \d to match any single digit. Use \s to match any single whitespace character.
^[a-zA-Z0-9]+ [a-zA-Z0-9]+$
… should do it.
This should do it:
^[0-9a-zA-Z]+ [0-9a-zA-Z]+$
Demo: http://jsfiddle.net/5m6RH/
Use this regex:
^[\p{L}\d]+ [\p{L}\d]+$
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