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.
  • 76,493
  • 17
  • 173
  • 144
0
str.replace(/,/g,"$");

will replace , with $

DEMO

Jankya
  • 966
  • 2
  • 12
  • 34
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
  • 20,733
  • 8
  • 41
  • 71
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
  • 19,138
  • 5
  • 33
  • 44