Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex, every non-alphanumeric character except white space or colon

Tags:

regex

People also ask

What is used to match anything except a whitespace?

You can match a space character with just the space character; [^ ] matches anything but a space character.

Is colon an alphanumeric?

space, percent sign, underscore, pipe, colon, semicolon, etc are non-alphanumeric characters. They can be also categorized as punctuation characters, symbol characters, etc.

What is the regex for white space?

The most common regex character to find whitespaces are \s and \s+ . The difference between these regex characters is that \s represents a single whitespace character while \s+ represents multiple whitespaces in a string.

Is colon a special regex character?

Colon does not have special meaning in a character class and does not need to be escaped.


[^a-zA-Z\d\s:]
  • \d - numeric class
  • \s - whitespace
  • a-zA-Z - matches all the letters
  • ^ - negates them all - so you get - non numeric chars, non spaces and non colons

This should do it:

[^a-zA-Z\d\s:]

If you want to treat accented latin characters (eg. à Ñ) as normal letters (ie. avoid matching them too), you'll also need to include the appropriate Unicode range (\u00C0-\u00FF) in your regex, so it would look like this:

/[^a-zA-Z\d\s:\u00C0-\u00FF]/g
  • ^ negates what follows
  • a-zA-Z matches upper and lower case letters
  • \d matches digits
  • \s matches white space (if you only want to match spaces, replace this with a space)
  • : matches a colon
  • \u00C0-\u00FF matches the Unicode range for accented latin characters.

nb. Unicode range matching might not work for all regex engines, but the above certainly works in Javascript (as seen in this pen on Codepen).

nb2. If you're not bothered about matching underscores, you could replace a-zA-Z\d with \w, which matches letters, digits, and underscores.


Try this:

[^a-zA-Z0-9 :]

JavaScript example:

"!@#$%* ABC def:123".replace(/[^a-zA-Z0-9 :]/g, ".")

See a online example:

http://jsfiddle.net/vhMy8/


If you mean "non-alphanumeric characters", try to use this:

var reg =/[^a-zA-Z0-9]/g      //[^abc]

No alphanumeric, white space or '_'.

var reg = /[^\w\s)]|[_]/g;