user1036103
user1036103

Reputation: 23

Javascript: How can I replace empty space with comma?

I want to replace html entity   with a comma.

Html string : 2 077 Ft

//existing script - not working - adding comma after every second character
txt = txt.replace(/(\d{2})/g,"$1,")

I want an out put like

txt = 2,077 Ft

Upvotes: 0

Views: 1242

Answers (2)

Nathan
Nathan

Reputation: 1520

Have you tried using string.replace(" ", ",");?

Edit:as pointed out in the comments it only does the first instance. To get over this you need to use regex. e.g.: string.replace(/ /g, ",");

Upvotes: 0

Develoger
Develoger

Reputation: 3998

var string = '2 077 Ft';
string = string.replace(/ /g, ',');

var modifiedElem = document.getElementById('modified');
modified.innerHTML = string;
<div id="original">2&nbsp;077 Ft</div>
<div id="modified"></div>

This will replace all occurances of '&nbsp' in desired string.

Upvotes: 3

Related Questions