25

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(/_/, " ") 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 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?
    – Gumbo
    Mar 18, 2011 at 18:18
  • How can I replace underscores with spaces using a regex in Javascript?
    – Karim Ali
    Mar 18, 2011 at 18:23

5 Answers 5

64

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(" ");
5
  • 2
    To quote Crescent Fresh quoting John Resig, it is actually more efficient currently doing the latter approach of .split("_").join(" ").
    – Justin
    Sep 10, 2014 at 6:53
  • I agree. I believe the first method will only replace the first underscore in the string Mar 6, 2015 at 3:46
  • 1
    @AdamJosephLooze Did you check it? The g flag takes care of replacing all occurrences.
    – pepkin88
    Mar 6, 2015 at 15:52
  • 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. Mar 6, 2015 at 19:12
  • @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)
    – pepkin88
    Mar 7, 2015 at 15:24
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)..

1
var str1="my__st_ri_ng";
var str2=str1.replace(/_/g, ' ');
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
  • You are right. Except the question didn't say anything about JavaScript when I wrote this answer. Jul 19, 2016 at 21:35
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"

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.