Your regular expression [^a-zA-Z0-9]\s/g says match any character that is not a number or letter followed by a space.
Remove the \s and you should get what you are after if you want a _ for every special character.
var newString = str.replace(/[^A-Z0-9]/ig, "_");
That will result in hello_world___hello_universe
If you want it to be single underscores use a + to match multiple
var newString = str.replace(/[^A-Z0-9]+/ig, "_");
That will result in hello_world_hello_universe
Your regular expression [^a-zA-Z0-9]\s/g says match any character that is not a number or letter followed by a space.
Remove the \s and you should get what you are after if you want a _ for every special character.
var newString = str.replace(/[^A-Z0-9]/ig, "_");
That will result in hello_world___hello_universe
If you want it to be single underscores use a + to match multiple
var newString = str.replace(/[^A-Z0-9]+/ig, "_");
That will result in hello_world_hello_universe
It was not asked precisely to remove accent (only special characters), but I needed to.
The solutions givens here works but they don’t remove accent: é, è, etc.
So, before doing epascarello’s solution, you can also do:
var newString = "développeur & intégrateur";
newString = replaceAccents(newString);
newString = newString.replace(/[^A-Z0-9]+/ig, "_");
alert(newString);
/**
* Replaces all accented chars with regular ones
*/
function replaceAccents(str) {
// Verifies if the String has accents and replace them
if (str.search(/[\xC0-\xFF]/g) > -1) {
str = str
.replace(/[\xC0-\xC5]/g, "A")
.replace(/[\xC6]/g, "AE")
.replace(/[\xC7]/g, "C")
.replace(/[\xC8-\xCB]/g, "E")
.replace(/[\xCC-\xCF]/g, "I")
.replace(/[\xD0]/g, "D")
.replace(/[\xD1]/g, "N")
.replace(/[\xD2-\xD6\xD8]/g, "O")
.replace(/[\xD9-\xDC]/g, "U")
.replace(/[\xDD]/g, "Y")
.replace(/[\xDE]/g, "P")
.replace(/[\xE0-\xE5]/g, "a")
.replace(/[\xE6]/g, "ae")
.replace(/[\xE7]/g, "c")
.replace(/[\xE8-\xEB]/g, "e")
.replace(/[\xEC-\xEF]/g, "i")
.replace(/[\xF1]/g, "n")
.replace(/[\xF2-\xF6\xF8]/g, "o")
.replace(/[\xF9-\xFC]/g, "u")
.replace(/[\xFE]/g, "p")
.replace(/[\xFD\xFF]/g, "y");
}
return str;
}
Run code snippetEdit code snippet Hide Results Copy to answer Expand
Source: https://gist.github.com/jonlabelle/5375315
I want to remove the special characters as well as extra spaces
Regular Expressions, How do you remove white space with special characters?
java - Remove spaces and special characters from string - Stack Overflow
One single Regex to Remove Special characters and multiple consecutive Spaces and Then Snake Case It - Stack Overflow
You can remove everything but the digits:
phone = phone.replaceAll("[^0-9]","");
To remove all non-digit characters you can use
replaceAll("\\D+",""); \\ \D is negation of \d (where \d represents digit)
If you want to remove only spaces, ( and ) you can define your own character class like
replaceAll("[\\s()]+","");
Anyway your problem was caused by fact that some of characters in regex are special. Among them there is ( which can represent for instance start of the group. Similarly ) can represent end of the group.
To make such special characters literals you need to escape them. You can do it many ways
"\\("- standard escaping in regex"[(]"- escaping using character class"\\Q(\\E"-\Qand\Ecreate quote - which means that regex metacharacters in this area should be treated as simple literalsPattern.quote("("))- this method usesPattern.LITERALflag inside regex compiler to point that metacharacters used in regex are simple literals without any special meaning
Replace any sequence of non-letters with a single white space:
str.replaceAll("[^a-zA-Z]+", " ")
You also might want to apply trim() after the replace.
If you want to support languages other than English, use "[^\\p{IsAlphabetic}]+" or "[^\\p{IsLetter}]+". See this question about the differences.
The OR operator (|) should work:
System.out.println(str.replaceAll("([^a-zA-Z]|\\s)+", " "));
Actually, the space doesn't have to be there at all:
System.out.println(str.replaceAll("[^a-zA-Z]+", " "));
This can be done without regex:
>>> string = "Special $#! characters spaces 888323"
>>> ''.join(e for e in string if e.isalnum())
'Specialcharactersspaces888323'
You can use str.isalnum:
S.isalnum() -> bool Return True if all characters in S are alphanumeric and there is at least one character in S, False otherwise.
If you insist on using regex, other solutions will do fine. However note that if it can be done without using a regular expression, that's the best way to go about it.
Here is a regex to match a string of characters that are not a letters or numbers:
[^A-Za-z0-9]+
Here is the Python command to do a regex substitution:
re.sub('[^A-Za-z0-9]+', '', mystring)
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, ""));
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, '');