I have a string. emoji-id can be different
<tg-emoji emoji-id="5818665600624365278">⏺</tg-emoji>
I want to leave only emoticons
⏺
Is it possible to do this using regular expressions in JS? How to do it?
CodePudding user response:
You can use a helper function that I call stripHTML (or stripTags) that does exactly that.
function stripHtml(html) {
var tmp = document.createElement("DIV");
tmp.innerHTML = html;
return tmp.textContent || tmp.innerText || "";
}
var html = '<tg-emoji emoji-id="5818665600624365278">⏺</tg-emoji>';
console.log(stripHtml(html))
CodePudding user response:
There could be a two scenarios :
- If It is a string, then you can achieve this by using
RegEx
with the help ofString.replace()
method.
const str = '<tg-emoji emoji-id="5818665600624365278">⏺</tg-emoji>';
const res = str.replace(/(<([^>] )>)/ig, '');
console.log(res);
- If input is a HTML, then you can get the
innerText
ortextContent
const res = document.getElementsByTagName("tg-emoji")[0].innerText;
console.log(res);
<tg-emoji emoji-id="5818665600624365278">⏺</tg-emoji>