skip to Main Content

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)

3

Answers


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

    Login or Signup to reply.
  2. Use t=t.replaceAll(', ',','); for replacing all ", " with ",".

    Login or Signup to reply.
  3. I feel like a simple google search would have come up with String.prototype.replaceAll() MDN.

    const 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))'
    const newT = t.replaceAll(', ',',') // newT is with all replaced
    console.log(t.length, newT.length)
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search