Home » Jquery » javascript – Remove single quotes and last comma in jquery string using Regex

javascript – Remove single quotes and last comma in jquery string using Regex

Posted by: admin February 22, 2020 Leave a comment

Questions:

I have a data

'abc','def','ghi',

I want to remove the single quote on all character and want to remove only the last comma, example like below

abc,def,ghi

How would i achieve that using regex for javascript?

I tried using this regex

.replace(^\'|,\s*$,"");

But seems like it is only removing the first quote as shown below

abc','def','ghi',

I am not very good in regex, i appreciate any help that i can get. Thanks

How to&Answer:

try this:

.replace(/\'|,$/g, "");

the ^ at the beggining made the regexp to only match the quote at the beggining of the string, also you have to add the g to keep looking after the first match

Answer:

There is an easy way, use the $ operator

.replace(/'|,$/g, '')

Answer:

Can you please check replace(/'|(,)$/g," ") and it should work.