Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

replace commas with dots and add element to string

I want to replace all commas of a (number) string with dots and add another element at the same time to display the currency

so far I have this

$("#myelement").text(function () {
     return $(this).text().replace(/\,/g, '.');
});

So far this works and returns for example 1,234,567 as 1.234.567 but how can I add a string/element to it so that I get 1.234.567 Dollars or 1.234.567 Rupis etc.

like image 522
ST80 Avatar asked Mar 13 '23 05:03

ST80


1 Answers

Just add + " Dollars" (or Rupees, etc.) to what you're returning from the function:

$("#myelement").text(function () {
     return $(this).text().replace(/\,/g, '.') + " Dollars";
});

Note that as georg points out, you don't need the $(this).text() part, the callback gets the index and the old text as arguments:

$("#myelement").text(function(index, text) {
     return text.replace(/\,/g, '.') + " Dollars";
});

Side note: , isn't special in regular expressions, no need to escape it (although doing so is harmless). So just /,/g, not /\,/g.

like image 86
T.J. Crowder Avatar answered Mar 24 '23 21:03

T.J. Crowder