Replace all special characters in javascript

I would like a RegExp that will remove all special characters from a string. I am trying something like this but it doesn’t work in IE7, though it works in Firefox.

var specialChars = "!@#$^&%*[]+=-[]\/{}|:?,.";

for [var i = 0; i < specialChars.length; i++] {
  stringToReplace = stringToReplace.replace[new RegExp["\\" + specialChars[i], "gi"], ""];
}

A detailed description of the RegExp would be helpful as well.

Toto

87k61 gold badges86 silver badges120 bronze badges

asked Dec 7, 2010 at 8:47

Timothy RuhleTimothy Ruhle

7,12710 gold badges40 silver badges64 bronze badges

10

var desired = stringToReplace.replace[/[^\w\s]/gi, '']

As was mentioned in the comments it's easier to do this as a whitelist - replace the characters which aren't in your safelist.

The caret [^] character is the negation of the set [...], gi say global and case-insensitive [the latter is a bit redundant but I wanted to mention it] and the safelist in this example is digits, word characters, underscores [\w] and whitespace [\s].

answered Dec 7, 2010 at 8:55

annakataannakata

73.1k16 gold badges113 silver badges180 bronze badges

9

Note that if you still want to exclude a set, including things like slashes and special characters you can do the following:

var outString = sourceString.replace[/[`~!@#$%^&*[]_|+\-=?;:'",.\{\}\[\]\\\/]/gi, ''];

take special note that in order to also include the "minus" character, you need to escape it with a backslash like the latter group. if you don't it will also select 0-9 which is probably undesired.

answered Jun 18, 2012 at 20:10

5

Plain Javascript regex does not handle Unicode letters.

Do not use [^\w\s], this will remove letters with accents [like àèéìòù], not to mention to Cyrillic or Chinese, letters coming from such languages will be completed removed.

You really don't want remove these letters together with all the special characters. You have two chances:

  • Add in your regex all the special characters you don't want remove,
    for example: [^èéòàùì\w\s].
  • Have a look at xregexp.com. XRegExp adds base support for Unicode matching via the \p{...} syntax.

var str = "Їжак::: résd,$%& adùf"
var search = XRegExp['[[^?\\pL ]+]'];
var res = XRegExp.replace[str, search, '',"all"];

console.log[res]; // returns "Їжак::: resd,adf"
console.log[str.replace[/[^\w\s]/gi, ''] ]; // returns " rsd adf"
console.log[str.replace[/[^\wèéòàùì\s]/gi, ''] ]; // returns " résd adùf"

answered Nov 27, 2016 at 17:25

freedevfreedev

22.5k7 gold badges102 silver badges117 bronze badges

3

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

Chủ Đề