I wrote an app that shows last posts through API, and I'm using a regex to strip HTML tags from the result (that is actually plain text).
post.contentHtml retrieve the last post
example retrieved: "hi this is my last post <b>with</b> some html tags"
so my line is:
post.contentHtml().replace(/<\/?[^>]+(>|$)/g, '')
example retrieved: "hi this is my last post with some html tags"
Now sometimes users post text with image URLs, and I want to replace all image URLs with a FontAwesome icons.
Is this possible with JavaScript?
found a solution by myself:
since i do not know how to replace with rendered html (actually if i put html it returns literally, i can do this with a text icon:
post.contentHtml().replace(/<\/?[^>]+(>|$)/g, '').replace(/(https?:\/\/\S+(\.png|\.jpg|\.gif))/g, '📷')
The simplest solution is to use match with regex. The sample is given below.
Regex:
/<[^>]*>/g
Note: You can make a more complex regex.
const str = 'hi this is <img src="test" /> my <span>with1</span> last post <b>with2</b> some html tags';
console.log(str.replace(/<[^>]*>/g, ""))