Coder
Coder

Reputation: 169

Javascript replace globally with regex

I have special characters in my XML and i am using Javascript function to replace it. I have found global search to replace in all my tags as.

 var res = outputText.replace(/&/g,"&"); 

But i also want to replace </Product> tag with <btn>Click NOW</btn></Product>

How can i replace this i am trying like

res = res.replace(/</Product>/g,"<btn>Click  NOW</btn></Product>"); 

But it is giving me error invalid regular expression flag P

Upvotes: 1

Views: 71

Answers (2)

Leo Nomdedeu
Leo Nomdedeu

Reputation: 338

You have to scape the / before the P

/<\/P

Upvotes: 1

gdoron
gdoron

Reputation: 150253

You need to escape the / with \:

res = res.replace(/<\/Product>/g,"<btn>Click  NOW</btn></Product>"); 
//------------------^

Upvotes: 3

Related Questions