23

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

var ZZZ = "This_is_my_name";
indiv
  • 16,628
  • 6
  • 58
  • 82
Karim Ali
  • 1,815
  • 4
  • 19
  • 29
  • Any hint on programming language? Regex doesn't replace anything by itself. `string.replace(/_/, " ")` – Marcel Jackwerth Mar 18 '11 at 18:17
  • Which language (looks like JavaScript)? This can be done without regex (but not in JavaScript ;)) In VIM: `:%s/_/ /g` – Felix Kling Mar 18 '11 at 18:17
  • 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 '11 at 18:18
  • How can I replace underscores with spaces using a regex in Javascript? – Karim Ali Mar 18 '11 at 18:23

4 Answers4

61

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(" ");
pepkin88
  • 2,704
  • 18
  • 19
  • 2
    To quote [Crescent Fresh](http://stackoverflow.com/a/441035/1778898) quoting [John Resig](http://ejohn.org/blog/javascript-micro-templating/), it is actually more efficient currently doing the latter approach of .split("_").join(" "). – Justin Sep 10 '14 at 06:53
  • I agree. I believe the first method will only replace the first underscore in the string – Adam Joseph Looze Mar 06 '15 at 03:46
  • 1
    @AdamJosephLooze Did you check it? The `g` flag takes care of replacing all occurrences. – pepkin88 Mar 06 '15 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. – Adam Joseph Looze Mar 06 '15 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 http://jsbin.com/) – pepkin88 Mar 07 '15 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)..

Jack
  • 128,551
  • 28
  • 227
  • 331
0
var str1="my__st_ri_ng";
var str2=str1.replace(/_/g, ' ');
Aamir Afridi
  • 6,233
  • 3
  • 39
  • 41
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/_/ /
Adam Batkin
  • 49,616
  • 8
  • 123
  • 114