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
Heres a snippet. Dk if it's what you're looking for..
var ad = '217Z2800343645@@@@@@@25.00'
var ada = ad.replace(/@/g, " ")
console.log(ada)
html ignores mutlible spaces
either use <pre></pre> tag to preserve spaces
or use nonbreakablespaces ( )
https://jsfiddle.net/agfwo8hu/
<pre>hello test</pre>
<p>hello test</p>
//decode function
function decode(encodedStr) {
return decodeURIComponent(encodedStr.replace(/\+/g, " "));
}
//encode function
function encode(unencoded ) {
return encodeURIComponent(unencoded).replace(/'/g,"%27").replace(/"/g,"%22");
}
You can also use the JQuery.params():
var myObject = {
stid: 'You are happy',
b: [ 1, 2, 3 ],
.......
};
var recursiveEncoded = $.param( myObject );
var recursiveDecoded = decodeURIComponent( $.param( myObject ) );
alert( recursiveEncoded );
alert( recursiveDecoded );
This should do the trick
var url = "http://localhost:8008/Login.aspx?stid=Are%20You%20Happy",
decoded_url = decodeURIComponent(url.replace(/\+/g, " "));
alert(decoded_url.split('stid=')[1]);
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, ""));
Run code snippetEdit code snippet Hide Results Copy to answer Expand
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, '');
Try .replace(/ /g,"_");
Edit: or .split(' ').join('_') if you have an aversion to REs
Edit: John Resig said:
If you're searching and replacing through a string with a static search and a static replace it's faster to perform the action with .split("match").join("replace") - which seems counter-intuitive but it manages to work that way in most modern browsers. (There are changes going in place to grossly improve the performance of .replace(/match/g, "replace") in the next version of Firefox - so the previous statement won't be the case for long.)
try this:
key=key.replace(/ /g,"_");
that'll do a global find/replace
javascript replace
Check this out: How to replace all occurrences of a string in JavaScript?
Short answer:
str.replace(/%20/g, " ");
EDIT: In this case you could also do the following:
decodeURI(str)
The percentage % sign followed by two hexadecimal numbers (UTF-8 character representation) typically denotes a string which has been encoded to be part of a URI. This ensures that characters that would otherwise have special meaning don't interfere. In your case %20 is immediately recognisable as a whitespace character - while not really having any meaning in a URI it is encoded in order to avoid breaking the string into multiple "parts".
Don't get me wrong, regex is the bomb! However any web technology worth caring about will already have tools available in it's library to handle standards like this for you. Why re-invent the wheel...?
var str = 'xPasswords%20do%20not%20match';
console.log( decodeURI(str) ); // "xPasswords do not match"
Javascript has both decodeURI and decodeURIComponent which differ slightly in respect to their encodeURI and encodeURIComponent counterparts - you should familiarise yourself with the documentation.
Remove the regular space that you have first in the pattern:
str = str.replace(/[\u00A0\u1680\u180e\u2000-\u2009\u200a\u200b\u202f\u205f\u3000]/g,'');
try this:
var str = "Hello this is a test of the site";
str= str.replace(/[\u00A0\u1680\u180e\u2000-\u2009\u200a\u200b\u202f\u205f\u3000]/g,'')
same as you did, but with out ' ' (regular space)
In ES2015/ES6 you can use the String.Prototype.normalize() method to decompose both characters to the same simple space character:
const normalize = str => str.normalize('NFKD');
console.log(normalize("item\u0020") === normalize("item\u00a0"));
The space in the first string is character code 160 (a non-breaking space), and the space in the second string is character code 32 (a normal space), so the strings aren't equal to each other.
console.log("item ".charCodeAt(4), "item ".charCodeAt(4));
is there a better approach to convert all special space characters with normal space?.
You can match space characters which aren't tabs or newlines and replace with a normal space:
const makeSpacesNormal = str => str.replace(/(?=\s)[^\r\n\t]/g, ' ');
console.log(makeSpacesNormal("item ") === makeSpacesNormal("item "));
Specifically, the \s will match a whole bunch of space-like characters:
[\t\n\v\f\r \u00a0\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u2028\u2029\u3000]
and by matching and replacing those (except newlines and tabs, if you want), you'll be left with ordinary spaces.