1

Below is my string.When I console b it is showing as below output:

var a='602,315,805,887,810,863,657,665,865,102,624,659,636';
var b = a.replace(',',"$");
console.log(b);

output:

602$315,805,887,810,863,657,665,865,102,624,659,636

What should I do to replace complete commas in string to $.

sivashanker
  • 55
  • 2
  • 8

4 Answers4

5

Use regexp, /,/g with global flag

var a ='602,315,805,887,810,863,657,665,865,102,624,659,636';
var b = a.replace(/,/g,"$");

Example

Oleksandr T.
  • 73,526
  • 17
  • 164
  • 143
0
str.replace(/,/g,"$");

will replace , with $

DEMO

Jankya
  • 956
  • 2
  • 11
  • 33
0

This question already has an answer anyway i have provide a different approach

var var a ='602,315,805,887,810,863,657,665,865,102,624,659,636';
var change= '$'
a= a.split(',').join(change);
Arunprasanth K V
  • 19,085
  • 8
  • 35
  • 68
0

You can use String methods .split() and .join() to make an array then glue the pieces back together.

var b = a.split(',').join('$');
Roamer-1888
  • 18,902
  • 5
  • 31
  • 44