0

I like to replace any tag with specific string then print them on console.log

var str ='<p><img src=""><img src=""><img src=""><div><img src=""></div><div></div><img src=""><div></div></p>'


var result = $(str).find('img').replaceWith("<$1>")

I was expecting something like

var str ='<p><$1><$1><$1><div><$1></div><div></div><$1><div></div></p>'

Would be great after converting them i like to print the result

이재찬
  • 56
  • 7

1 Answers1

1

You can use .html() and some regexes to get you there. Granted, using regex to read HTML will always lead to sadness but sometimes people still do it in when they have no other option.

const originalHtml = $('#some-container').html()

const convertedHtml = originalHtml.replace(/<p>/g, '<foo>').replace(/<\/p>/g, '</foo>')

Example fiddle

Andrew Smith
  • 1,434
  • 13
  • 29