25

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

var ZZZ = "This_is_my_name";
indiv
  • 17,306
  • 6
  • 61
  • 82
Karim Ali
  • 2,243
  • 6
  • 23
  • 31
  • 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
  • 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 '11 at 18:18
  • How can I replace underscores with spaces using a regex in Javascript? – Karim Ali Mar 18 '11 at 18:23

5 Answers5

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(" ");
pepkin88
  • 2,742
  • 20
  • 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
var str1="my__st_ri_ng";
var str2=str1.replace(/_/g, ' ');
Aamir Afridi
  • 6,364
  • 3
  • 42
  • 42
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
  • 131,802
  • 30
  • 241
  • 343
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
  • 51,711
  • 9
  • 123
  • 115
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"
anilam
  • 694
  • 6
  • 7