-1

I have anchor tags in my html. The value of href attribute starts with either ' or ". I want to replace javascript:window.location.replace(' href value and ').

sample_input

"<a href="xyz.com" attr="pqrs"> text </a> \n <a href='xyz1.com' attr="pqrs"> text1 </a>"

desired_output

"<a href="javascript:window.location.replace('xyz.com')" attr="pqrs"> text </a> \n <a href='"xyz.com"' attr="pqrs"> text1 </a>"

1 Answers1

2

Try the below code.

sample_input.replace(new RegExp('(<a.?href=)([\'\"])(.?)([\'\"])', 'g'), "\$1\"javascript:window.location.replace('\$3')\"")
Karmveer Singh
  • 939
  • 5
  • 16