Simple Regex -- Replace Underscore with a Space

How to replace underscores with spaces using a regex in Javascript

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(" ");

simple regex -- replace underscore with a space

Whoops, I actually had it working--just forgot to update the variable name :P

I was using this:

@id = params[:id]
@title = @id.gsub("_", " ")

Replace initial spaces with underscores in Javascript

I want to replace each of the leading spaces with an underscore

To do that with just one replace call within the current spec,* you'd need the function call version of replace to do that, creating a string of underscores as long as the matched sequence of spaces:

y = x.replace(/^ +/, function(m) {
return "_".repeat(m.length);
});

or with an ES2015+ arrow function:

y = x.replace(/^ +/, m => "_".repeat(m.length));

Live Example:

const x = "    four spaces";const y = x.replace(/^ +/, m => "_".repeat(m.length));console.log(y);

Replace multiple underscore with space or new line on paragraph using regex

const text = "The future of mobile gameplay is War Games!_______________________________________ATTENTION ALL PLAYERS! We’d love to hear your feedback to help us improve the game. To leave feedback visit here.";

let newstr = text.replace(/\_+/i, "\n");
console.log(newstr);

c# Regex.Replace [^\w ] that also removes underscores?

This should work for you

// Expression: _|[^\w\d ]
cleaned_string = Regex.Replace(input_string, @"/_|[^\w\d ]", "");

How can I regex replace all spaces in matched text with underscore? (Javascript)

Use a function in the replace() method. That function can perform a nested replacement on that capture group.

let str = `enum class test{    something1, // this is a comment    something2, // something else    something3,};`;
let new_str = str.replace(/\,.*\/\/\s*(.*)/g, (match0, match1) => ' | ' + match1.replace(/ /g, '_'));console.log(new_str);

Replacing spaces with underscores in JavaScript?

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

Regex, replace all underscore and special char with spaces?

Underscore is considered a "word character" in PCRE regular expressions. If what you want to match is "anything that is not a word character or an underscore", try this:

new_str = re.sub(r'[\W_]', ' ', new_str)


Related Topics



Leave a reply



Submit