Home > OS >  How to replace tag with different id in string?
How to replace tag with different id in string?

Time:10-17

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 :

  1. If It is a string, then you can achieve this by using RegEx with the help of String.replace() method.

const str = '<tg-emoji emoji-id="5818665600624365278">⏺</tg-emoji>';

const res = str.replace(/(<([^>] )>)/ig, '');

console.log(res);

  1. If input is a HTML, then you can get the innerText or textContent

const res = document.getElementsByTagName("tg-emoji")[0].innerText;

console.log(res);
<tg-emoji emoji-id="5818665600624365278">⏺</tg-emoji>

  • Related