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
How to Create a Form in Rails Without Having to Use Form_For and a Model Instance
Run Code Only If Script Called from the Command Line
How to Make a Ruby Script Run Once a Second
How to Fix Libv8 Error from Gemfile on Mavericks
Implementing Bayesian Classifier in Ruby
"Gem Update --System Is Disabled on Debian" Error
Adding a "Like/Unlike" Button to a Post in Rails
Best Way to Work with Large Amounts of CSV Data Quickly
How to Deal with Ruby 2.1.2 Memory Leaks
Devise Nomethoderror 'For' Parametersanitizer
Add a Callback Function to a Ruby Array to Do Something When an Element Is Added
In a Sinatra App on Heroku, Session Is Not Shared Across Dynos
Ruby Find and Return Objects in an Array Based on an Attribute