26

How can I replace underscores with spaces using a regex in Javascript?

var ZZZ = "This_is_my_name";
4
  • Any hint on programming language? Regex doesn't replace anything by itself. string.replace(/_/, " ") Commented Mar 18, 2011 at 18:17
  • Which language (looks like JavaScript)? This can be done without regex (but not in JavaScript ;)) In VIM: :%s/_/ /g Commented Mar 18, 2011 at 18:17
  • 1
    Regular expressions can’t replace anything. They can only describe some grammar. But many language use regular expressions to search for certain strings. So what language do you use? Commented Mar 18, 2011 at 18:18
  • How can I replace underscores with spaces using a regex in Javascript? Commented Mar 18, 2011 at 18:23

5 Answers 5

65

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(" ");
Sign up to request clarification or add additional context in comments.

5 Comments

To quote Crescent Fresh quoting John Resig, it is actually more efficient currently doing the latter approach of .split("_").join(" ").
I agree. I believe the first method will only replace the first underscore in the string
@AdamJosephLooze Did you check it? The g flag takes care of replacing all occurrences.
ya. i had a list of cities, and it only changed the underscore in the first instance. so i used split join and it worked for me. but i see everybody else is using the same replace, so it must work properly, but it did not for me. i ran mine in a for loop to print out all cities and it only worked with first instance.
@AdamJosephLooze Both of my snippets are compliant with ES3, ES5 (and higher) specs. If you had a problem with the replace method with a regular expression with a g flag (that flag is important), that may be because of a faulty JS engine implementation or because of a mistake in the code. Could you show me your code, maybe I could help? (maybe with jsbin.com)
1

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

Comments

1
var str1="my__st_ri_ng";
var str2=str1.replace(/_/g, ' ');

Comments

0

Replace "_" with " "

The actual implementation depends on your language.

In Perl it would be:

s/_/ /g

But the truth is, if you are replacing a fixed string with something else, you don't need a regular expression, you can use your language/library's basic string replacement algorithms.

Another possible Perl solution would be:

tr/_/ /

1 Comment

You are right. Except the question didn't say anything about JavaScript when I wrote this answer.
0

To replace the underscores with spaces in a string, call the replaceAll() method, passing it an underscore and space as parameters, e.g. str.replaceAll('_', ' '). The replaceAll method will return a new string where each underscore is replaced by a space.

const str = 'apple_pear_melon';

// ✅ without regular expression
const result1 = str.replaceAll('_', ' ');
console.log(result1); // 👉️ "apple pear melon"

// ✅ with regular expression
const result2 = str.replace(/_+/g, ' ');
console.log(result2); // 👉️ "apple pear melon"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.