Remove all special characters except space from a string using JavaScript

JavascriptSpecial Characters

Javascript Problem Overview


I want to remove all special characters except space from a string using JavaScript.

For example, abc's test#s should output as abcs tests.

Javascript Solutions


Solution 1 - Javascript

You should use the string replace function, with a single regex. Assuming by special characters, you mean anything that's not letter, here is a solution:

const str = "abc's test#s";
console.log(str.replace(/[^a-zA-Z ]/g, ""));

Solution 2 - Javascript

You can do it specifying the characters you want to remove:

string = string.replace(/[&\/\\#,+()$~%.'":*?<>{}]/g, '');

Alternatively, to change all characters except numbers and letters, try:

string = string.replace(/[^a-zA-Z0-9]/g, '');

Solution 3 - Javascript

The first solution does not work for any UTF-8 alphabet. (It will cut text such as Привіт). I have managed to create a function which does not use RegExp and use good UTF-8 support in the JavaScript engine. The idea is simple if a symbol is equal in uppercase and lowercase it is a special character. The only exception is made for whitespace.

function removeSpecials(str) {
    var lower = str.toLowerCase();
    var upper = str.toUpperCase();
    
    var res = "";
    for(var i=0; i<lower.length; ++i) {
        if(lower[i] != upper[i] || lower[i].trim() === '')
            res += str[i];
    }
    return res;
}

Update: Please note, that this solution works only for languages where there are small and capital letters. In languages like Chinese, this won't work.

Update 2: I came to the original solution when I was working on a fuzzy search. If you also trying to remove special characters to implement search functionality, there is a better approach. Use any transliteration library which will produce you string only from Latin characters and then the simple Regexp will do all magic of removing special characters. (This will work for Chinese also and you also will receive side benefits by making Tromsø == Tromso).

Solution 4 - Javascript

I don't know JavaScript, but isn't it possible using regex?

Something like [^\w\d\s] will match anything but digits, characters and whitespaces. It would be just a question to find the syntax in JavaScript.

Solution 5 - Javascript

search all not (word characters || space):

str.replace(/[^\w ]/, '')

Solution 6 - Javascript

I tried Seagul's very creative solution, but found it treated numbers also as special characters, which did not suit my needs. So here is my (failsafe) tweak of Seagul's solution...

//return true if char is a number
function isNumber (text) {
  if(text) {
    var reg = new RegExp('[0-9]+$');
    return reg.test(text);
  }
  return false;
}

function removeSpecial (text) {
  if(text) {
    var lower = text.toLowerCase();
    var upper = text.toUpperCase();
    var result = "";
    for(var i=0; i<lower.length; ++i) {
      if(isNumber(text[i]) || (lower[i] != upper[i]) || (lower[i].trim() === '')) {
        result += text[i];
      }
    }
    return result;
  }
  return '';
}

Solution 7 - Javascript

Try to use this one

var result= stringToReplace.replace(/[^\w\s]/g, '')

[^] is for negation, \w for [a-zA-Z0-9_] word characters and \s for space, /[]/g for global

Solution 8 - Javascript

const str = "abc's@thy#^g&test#s";
console.log(str.replace(/[^a-zA-Z ]/g, ""));

Solution 9 - Javascript

dot (.) may not be considered special. I have added an OR condition to Mozfet's & Seagull's answer:

function isNumber (text) {
      reg = new RegExp('[0-9]+$');
      if(text) {
        return reg.test(text);
      }
      return false;
    }
   
function removeSpecial (text) {
  if(text) {
    var lower = text.toLowerCase();
    var upper = text.toUpperCase();
    var result = "";
    for(var i=0; i<lower.length; ++i) {
      if(isNumber(text[i]) || (lower[i] != upper[i]) || (lower[i].trim() === '') || (lower[i].trim() === '.')) {
        result += text[i];
      }
    }
    return result;
  }
  return '';
}

Solution 10 - Javascript

Try this:

const strippedString = htmlString.replace(/(<([^>]+)>)/gi, "");
console.log(strippedString);

Solution 11 - Javascript

using below code you can remove the specific special characters

const input = `#if_1 $(PR_CONTRACT_END_DATE) == '23-09-2019' # 
Test27919<[email protected]> #elseif_1 $(PR_CONTRACT_START_DATE) ==  '20-09-2019' #
Sender539<[email protected]> #elseif_1 $(PR_ACCOUNT_ID) == '1234' #
AdestraSID<[email protected]> #else_1#Test27919<[email protected]>#endif_1#`;
const replaceString = input.split('$(').join('->').split(')').join('<-');


console.log(replaceString.match(/(?<=->).*?(?=<-)/g));

Solution 12 - Javascript

Whose special characters you want to remove from a string, prepare a list of them and then user javascript replace function to remove all special characters.

var str = 'abc'de#;:sfjkewr47239847duifyh';
alert(str.replace("'","").replace("#","").replace(";","").replace(":",""));

or you can run loop for a whole string and compare single single character with the ASCII code and regenerate a new string.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionnithiView Question on Stackoverflow
Solution 1 - JavascriptPetar IvanovView Answer on Stackoverflow
Solution 2 - JavascriptLakshmana Kumar DView Answer on Stackoverflow
Solution 3 - JavascriptSeagullView Answer on Stackoverflow
Solution 4 - JavascriptThiago MoraesView Answer on Stackoverflow
Solution 5 - JavascriptdovidView Answer on Stackoverflow
Solution 6 - JavascriptMozfetView Answer on Stackoverflow
Solution 7 - JavascriptShrinivasanView Answer on Stackoverflow
Solution 8 - JavascriptAbdullah Al MamunView Answer on Stackoverflow
Solution 9 - Javascripthassanf1View Answer on Stackoverflow
Solution 10 - JavascriptMuneeb AhmedView Answer on Stackoverflow
Solution 11 - Javascriptsudheer nunnaView Answer on Stackoverflow
Solution 12 - JavascriptGaurav AgrawalView Answer on Stackoverflow