Try .replace(/ /g,"_");
Edit: or .split(' ').join('_') if you have an aversion to REs
Edit: John Resig said:
Answer from Crescent Fresh on Stack OverflowIf 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 .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
How to replace all spaces present in a string with underscore using javascript? - Stack Overflow
javascript - How to replace underscores with spaces? - Stack Overflow
How to replace underscores with spaces using a regex in Javascript - Stack Overflow
javascript - Remove special symbols and extra spaces and replace with underscore using the replace method - Stack Overflow
You can replace all underscores in a string with a space using String.prototype.replace with a global regular expression:
str.replace(/_/g, ' ')
So just do that before the content is put in. Or, if you need to perform the replacement afterwards, the jquery solution is:
$('.name').text((i, oldText) =>
oldText.replace(/_/g, ' '));
which is equivalent to this plain DOM API loop:
for (const element of document.getElementsByClassName('name')) {
element.textContent = element.textContent.replace(/_/g, ' ');
}
ES2021 introduced the nifty replaceAll()-function which means it can be written as:
str.replaceAll('_', ' ')
If you want to do multiple elements just loop over them and use forEach():
const elements = document.querySelectorAll('.name')
elements.forEach(e => e.innerText = e.innerText.replaceAll('_', ' '))
If it is a JavaScript code, write this, to have transformed string in ZZZ2:
var ZZZ = "This_is_my_name";
var ZZZ2 = ZZZ.replace(/_/g, " ");
also, you can do it in less efficient, but more funky, way, without using regex:
var ZZZ = "This_is_my_name";
var ZZZ2 = ZZZ.split("_").join(" ");
Regular expressions are not a tool to replace texts inside strings but just something that can search for patterns inside strings. You need to provide a context of a programming language to have your solution.
I can tell you that the regex _ will match the underscore but nothing more.
For example in Groovy you would do something like:
"This_is_my_name".replaceAll(/_/," ")
===> This is my name
but this is just language specific (replaceAll method)..
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;
}
Source: https://gist.github.com/jonlabelle/5375315
DEMO
$("#test").keyup(function () {
var textValue = $(this).val();
textValue =textValue.replace(/ /g,"_");
$(this).val(textValue);
});
Update
DEMO
$("#test").keyup(function () {
this.value = this.value.replace(/ /g, "_");
});
Simply try this Demo
$("#test").keyup(function() {
if ($(this).val().match(/[ ]/g, "") != null) {
$(this).val($(this).val().replace(/[ ]/g, "_"));
}
});