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

3
  • $(str) won't even work because that is not a valid selector Commented Dec 12, 2019 at 1:01
  • ah.... is there way to bring whole selector then convert them? Commented Dec 12, 2019 at 1:27
  • You'll need to add them into the html itself and then do the select thing. You will need style="display: none;" for the parent p element to hide them all Commented Dec 12, 2019 at 1:32

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.