Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove not alphanumeric characters from string

People also ask

How do you remove non alphanumeric characters from a string?

A common solution to remove all non-alphanumeric characters from a String is with regular expressions. The idea is to use the regular expression [^A-Za-z0-9] to retain only alphanumeric characters in the string. You can also use [^\w] regular expression, which is equivalent to [^a-zA-Z_0-9] .

How do you get rid of non alphanumeric?

Non-alphanumeric characters can be remove by using preg_replace() function. This function perform regular expression search and replace. The function preg_replace() searches for string specified by pattern and replaces pattern with replacement if found.

How do you remove a non alphanumeric character from a string in Python?

Use the isalnum() Method to Remove All Non-Alphanumeric Characters in Python String. We can use the isalnum() method to check whether a given character or string is alphanumeric or not. We can compare each character individually from a string, and if it is alphanumeric, then we combine it using the join() function.

How do you remove all non alphanumeric characters from a string excel?

Select the range that you need to remove non-alphanumeric characters from, and click Kutools > Text > Remove Characters. 2. Then a Delete Characters dialog box will appear, only check Non-alphanumeric option, and click the Ok button. Now all of the non-alphanumeric characters have been deleted from the text strings.


Removing non-alphanumeric chars

The following is the/a correct regex to strip non-alphanumeric chars from an input string:

input.replace(/\W/g, '')

Note that \W is the equivalent of [^0-9a-zA-Z_] - it includes the underscore character. To also remove underscores use e.g.:

input.replace(/[^0-9a-z]/gi, '')

The input is malformed

Since the test string contains various escaped chars, which are not alphanumeric, it will remove them.

A backslash in the string needs escaping if it's to be taken literally:

"\\test\\red\\bob\\fred\\new".replace(/\W/g, '')
"testredbobfrednew" // output

Handling malformed strings

If you're not able to escape the input string correctly (why not?), or it's coming from some kind of untrusted/misconfigured source - you can do something like this:

JSON.stringify("\\test\red\bob\fred\new").replace(/\W/g, '')
"testredbobfrednew" // output

Note that the json representation of a string includes the quotes:

JSON.stringify("\\test\red\bob\fred\new")
""\\test\red\bob\fred\new""

But they are also removed by the replacement regex.


All of the current answers still have quirks, the best thing I could come up with was:

string.replace(/[^A-Za-z0-9]/g, '');

Here's an example that captures every key I could find on the keyboard:

var string = '123abcABC-_*(!@#$%^&*()_-={}[]:\"<>,.?/~`';
var stripped = string.replace(/[^A-Za-z0-9]/g, '');
console.log(stripped);

Outputs: '123abcABC'.


The problem is not with how you replace the characters, the problem is with how you input the string.

It's only the first backslash in the input that is a backslash character, the others are part of the control characters \r, \b, \f and \n.

As those backslashes are not separate characters, but part of the notation to write a single control characters, they can't be removed separately. I.e. you can't remove the backslash from \n as it's not two separate characters, it's the way that you write the control character LF, or line feed.

If you acutally want to turn that input into the desired output, you would need to replace each control character with the corresponding letter, e.g. replace the character \n with the character n.

To replace a control character you need to use a character set like [\r], as \r has a special meaning in a regular expression:

var input = "\\test\red\bob\fred\new";

var output = input
    .replace(/[\r]/g, 'r')
    .replace(/[\b]/g, 'b')
    .replace(/[\f]/g, 'f')
    .replace(/[\n]/g, 'n')
    .replace(/\\/g, '');

Demo: http://jsfiddle.net/SAp4W/


You can try this regex:

value.replace(/[\W_]/g, '');

To include Arabic letters alongside with English letters, you can use:

// Output: نصعربي
"ن$%^&*(ص ع___ربي".replace(/[^0-9a-z\u0600-\u06FF]/gi, '');