-4

Say we have:

Saudi Arabia

If I do:

loc.replace(/ /g,''));

Becomes:

SaudiArabia

What if I want to replace the white space with &nbsp?

Tried:

loc.replace(/ /g,'&nbsp')

I am looking for

Saudi&nbspArabia
rob.m
  • 9,843
  • 19
  • 73
  • 162
  • @T.J.Crowder it's not tho, if I then output loc i get `data-value="SaudiArabia"` – rob.m Jun 05 '18 at 09:39
  • 1
    That works just fine: http://jsbin.com/vogixewogo/edit?html,js,output. (Side note: It's ` `, not just `&nbsp`.) See also [the duplicate](https://stackoverflow.com/questions/1433212/replace-method-doesnt-work), if you're really doing what you've shown (but then your first one wouldn't work either). If that's not the problem, please update the question with details. – T.J. Crowder Jun 05 '18 at 09:39
  • 1
    [mcve] please – either you forgot to remove the first regex, or the string doesn't contain what you think it contains, or something sanitizes the string before placing it into the attribute, or any other reason that can't really be guessed without seeing the code. – JJJ Jun 05 '18 at 09:42
  • @JJJ the answer actually does what I am asking.. – rob.m Jun 05 '18 at 09:43
  • @rob.m - Ah, good! :-) – T.J. Crowder Jun 05 '18 at 09:43
  • That means that the space between the words isn't a "normal" space, otherwise `/ /g` would have worked as well. – JJJ Jun 05 '18 at 09:43
  • @JJJ yes it's what I am thinking, because while the answer shows the correct result on here, on my real case I am stille seeing SaudiArabia with no space, and I need to create the space with html special character not just " " – rob.m Jun 05 '18 at 09:45

1 Answers1

1

Did you mean this

var loc = "soudi arabia";
loc = loc.replace(/\s/g,' ');
console.log(loc)
Akhil Aravind
  • 5,741
  • 16
  • 35