Remove all special characters except space from a string using JavaScript
Asked Answered
R

13

269

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.

Riedel answered 2/7, 2011 at 4:52 Comment(2)
How do you define special character?Varmint
With regular expression var = string.replace(/[^\w\s]/gi, ''); Live Example : helpseotools.com/text-tools/remove-special-charactersColpotomy
A
563

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, ""));
Applewhite answered 2/7, 2011 at 5:1 Comment(7)
To use this solution on non-latin alphabet please check this answer outTrefor
This will also removed numerical characters!Reunionist
Actually you need this str.replace(/[^a-zA-Z0-9 ]/g, ""); notice there's a space between 0-9 and ]Frambesia
It need some tweak, it didn't remove / or - characters and the first character of camelCase supposed to be in lower case, but this one in uppercase.Vigor
This leaves stuff like: pspanspanstrongItem SKUstrongnbspspanspanKBRspanspanpp This is a bit better.Siftings
This does not work for Arabic, Chinese, German, French etc special symbolsMilker
this does not remove carriage return \nSixtyfourmo
T
210

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, '');
Tendance answered 4/6, 2013 at 9:13 Comment(6)
what if I don't wanna remove space using this? /[^a-zA-Z0-9]/gScourings
@ArbazAbid to keep space just add that to white listed characters. your regex would be "/[^a-zA-Z0-9 ]/g" (notice whitespace after 9)Herrle
This was the answer that worked, if a space is required.Lycanthrope
this replaces () and whitespaces in string .text().replace(/[ ()]/g, '')Swithbart
What does that regex represent? anything but alphanumeric chars? What is the negation symbol in regex?Sweep
@Sweep It represents everything that is not (^) letters (a-zA-Z), numbers (0-9) and a space ( ). So the negation would be something like "replace everything that is not letters, numbers and space by...".Holmium
M
35

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).

Mumble answered 21/10, 2014 at 8:54 Comment(4)
Thank you for this quite creative solution. It is much more in line with how languages actually work, since many of us don't consider "Привіт" or "æøå" special characters. Most solutions out there cut any character that isn't part of the English alphabet.Mentholated
Almost the perfect answer for me, but unfortunately it considers Chinese characters to be special characters.Undone
@EricMajerus and hindi tooNary
Be careful, this also considers numbers as special characters.Abbottson
N
16

search all not (word characters || space):

str.replace(/[^\w ]/, '')
Neddie answered 24/1, 2018 at 19:0 Comment(2)
perfect! removing all special unicode characters from the string.Worked
This was a nice solution I used for searching through first+last name. It handles accented characters (é) as well. I added a apostrophe to handle names like D'Angelo.Swinford
A
14

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.

Ark answered 2/7, 2011 at 5:2 Comment(1)
https://mcmap.net/q/24958/-remove-all-special-characters-with-regexp This is the answer to your question :)Tillietillinger
P
11

const str = "abc's@thy#^g&test#s";
console.log(str.replace(/[^a-zA-Z ]/g, ""));
Pattipattie answered 10/10, 2020 at 23:59 Comment(3)
pls, add explaination of what u didHumboldt
Numbers are not special characters and they will be deletedRothrock
Letters like ąčęėįšųūž may not be "special" characters in some context and they will be removed. Also all japanese/chinese and other languages with local letters. This code will keep only english alphabet a-z and A-Z.Rockefeller
S
10

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 '';
}
Sladen answered 17/2, 2016 at 11:45 Comment(1)
You forgot to declare reg in your first function.Recount
L
8

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

Lindner answered 18/11, 2019 at 15:26 Comment(0)
C
7

With regular expression

let string  = "!#This tool removes $special *characters* /other/ than! digits, characters and spaces!!!$";

var NewString= string.replace(/[^\w\s]/gi, '');
console.log(NewString); 

Result //This tool removes special characters other than digits characters and spaces

Live Example : https://helpseotools.com/text-tools/remove-special-characters

Colpotomy answered 6/9, 2022 at 9:30 Comment(0)
S
0

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 '';
}
Scoles answered 11/12, 2017 at 7:54 Comment(0)
C
0

Try this:

const strippedString = htmlString.replace(/(<([^>]+)>)/gi, "");
console.log(strippedString);
Chitwood answered 10/9, 2020 at 14:32 Comment(1)
This would leave the ' and # in the stripped string. Example output: "abc's test#s"Neptunium
S
-1

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));
Sandysandye answered 16/1, 2020 at 10:10 Comment(0)
G
-17

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.

Gormley answered 2/7, 2011 at 4:58 Comment(4)
No, please don't do that. It's really terribly slow. It is much, much better to use a regular expression.Varmint
This solution is very nice for replacing just one character. In my case it was helpful, thanks.Dunn
Instead of using replace method many times, please use it once only using regular expressions.Schoolroom
If more characters come up this solution fails, regular exp is generic way of doing this and is fastHortatory

© 2022 - 2024 — McMap. All rights reserved.