Bhumi Shah
Bhumi Shah

Reputation: 9476

javascript replace single quote with double single quote issue

I have used below syntzx to replace single quote with double single quote issue

str.replace(/'/g,"''");

but it's replace everytime when it load page. like

I have text

" Test's and test's page and test's event"

then first time ,it will be

" Test''s and test''s page and test''s event"

then again

" Test'''s and test'''s page and test'''s event"

then next loading

" Test''''s and test''''s page and test''''s event"

can you please help to get just single to double single quote only?

Upvotes: 0

Views: 2188

Answers (1)

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324790

If it's safe to assume that there won't be three or more quotes in a row, try this:

str.replace(/'+/g,"''")

If the assumption is not safe, and you just want to replace "a quote by itself" with two quotes, leaving multi-quotes alone, try this:

str.replace(/''?('*)/g,"''$1");

That being said, you might want to look into why it's replacing more than once in the first place ;)

Upvotes: 2

Related Questions