. Advertisement .
..3..
. Advertisement .
..4..
Many readers have been curious about the quickest methods to remove all non-alphanumeric characters in Javascript – which is why this detailed guideline was compiled. Here, you will find three different solutions for the issue.
How to Remove All Non-Alphanumeric Characters in Javascript?
There are three methods:
- Method 1. Use traditional removal commands
- Method 2. Convert the strings via JSON.Stringify()
- Method 3. Use \u0600-\u06FF if there is more than one language.
Method 1. Use Traditional Javascript Removal Commands
We discuss this method first because it’s the most straightforward. All you need is to simply discard all redundant elements except for the numbers and English alphabets (small and capital). The modifier “g” says global, while the “i” alludes to case insensitivity.
var input = '234bcdBCD-_*(!@#$%^&*()_-={}[]:"<>,.?/~`';
var stripped_string = input.replace(/[^a-z0-9]/gi, '');
console.log(stripped_string);
Output:
234bcdBCD
You can also optimize your regex by substituting it for /[\W]/g. in which \W equals [^0-9a-zA-Z]. Hence, the final regex will be /[\W_]/g since the input string’s underscore has already been removed.
Check out this code:
var input = '234bcdBCD-_*(!@#$%^&*()_-={}[]:"<>,.?/~`';
var stripped_string = input.replace(/[\W_]/g, '')
console.log(stripped_string);
Output:
234bcdBCD
Method 2. Use The Function JSON.Stringify() For String Conversion
Suppose you have one array containing several elemental strings. In these cases, it’s possible to pick the command JSON.Stringify() and convert these into strings. Then, based on your provided regex, replace them.
var input = ['234bcdBCD-_*(!@#$%^&*', 'ABC()_-={}[]:"<>,.?/~`'];
var stripped_string = JSON.stringify(input).replace(/[\W_]/g, '')
console.log(stripped_string);
Output:
234bcdBCDBCD
Method 3. Use The Command \u0600-\u06FF for Removal Except Alphanumeric
Let’s say you wish to remove all elements except alphanumeric; however, this time, you also have English and Arabic alphabets. What to do then? For clear identifications of Arabic letters, it’s a must to use \u0600-\u06FF, a relative block scope from Unicode.
var input = 'ن$%^&*(ص ع___ربي-bcd234_*(!@#$%^&*()_-={}[]:"<>,.?/~`';
var stripped_string = input.replace(/[^0-9a-z\u0600-\u06FF]/gi, '')
console.log(stripped_string);
Output:
"نصعربيbcd234"
Let’s stretch our imagination further; imagine having to work with more than two languages and seeking relative block ranges for every language in Unicode. So time-consuming, isn’t it?
Instead, /[^\p{L}\d]/gu can come in handy. The \p{L} will identify letters from all languages, while \d focuses on numbers.
G (global) replaces elements globally. On the other hand, a Unicode escape sequence will be identified via Unicode (U), with the symbol “^” assisting in character set negotiation. Hence, our final regex /[^\p{L}\d]/gu involves thorough replacement to negate a given set of characters.
function nonAlphaNumericRemoval(input) {
return input.replace(/[^\p{L}\d]/gu, '')
}
input_string = [
'#$zdso6poiud?',
'%^iYpeFß^ßI$jI',
'*(无论4如何?!',
'[фв@#еm1]'
]
for (var input of input_string) {
console.log(nonAlphaNumericRemoval(input))
}
Output:
"zdso6poiud"
"iYpeFßßIjI"
"无论4如何"
"фвеm1"
Conclusion
Now you know how to remove all non-alphanumeric characters in Javascript with ease – even when there are two different languages. To remove other elements in Javascript (such as substrings), feel free to browse this website for guidance.
Leave a comment