0

for the below posted string, i want to replace any occurances of , with ,

i mean that the resultant string should contains values separated only by comma not a comma plus space.

the result of the below posted code is:

POLYGON((595117.1423555185 5784603.23154566,595123.4425648759 5784792.817361793, 595123.4467946623 5784792.959964871, 595131.3773480996 5785096.90776269, 595137.0802208507 5785261.194424775, 595196.9548982648 5785262.269346152, 595198.8303912097 5785262.391339741))

as shown in the latter string, the comma plus a space was removed only in the first occurance in

595117.1423555185 5784603.23154566,595123.4425648759 5784792.817361793

but for the further occurances of a comma plus a space, they did not changed at all

please let me know how to replace all occurances of , with ,

code:

let t = 'POLYGON((595117.1423555185 5784603.23154566, 595123.4425648759 5784792.817361793, 595123.4467946623 5784792.959964871, 595131.3773480996 5785096.90776269, 595137.0802208507 5785261.194424775, 595196.9548982648 5785262.269346152, 595198.8303912097 5785262.391339741))'
t=t.replace(', ',',')
console.log(t)
Amrmsmb
  • 1
  • 27
  • 104
  • 226
  • In the comments to the other answer you say that `t.replaceAll` results in `TypeError: t.replaceAll is not a function` , if this is true then your above code is not what you actually have in your project or you use an old and outdated version of node (which should be mentioned in the question). Because `replaceAll` clearly works for your shown code ([see jsfiddle](https://jsfiddle.net/5te1s6f3/)) – t.niese Jul 28 '23 at 08:39

2 Answers2

0

.replace will always replace just 1 you can use .replaceAll instead Here is a link for it replaceAll MDN

Zhivko Nikolov
  • 145
  • 1
  • 11
0

Use t=t.replaceAll(', ',','); for replacing all ", " with ",".

kzi
  • 186
  • 1
  • 8
  • but i cant use replaceAll ..it says: TypeError: t.replaceAll is not a function – Amrmsmb Jul 28 '23 at 08:36
  • I tested it in my browser console. If `t` is a actually a string, `t.replaceAll` should be a valid function. – kzi Jul 28 '23 at 09:07