如何删除所有尖括号和使用JS内的一切?

我有HTML元素的字符串,如何删除所有尖括号和使用JS内的一切?

let test = `<figure><img alt="" src="some_image.jpg"><figcaption>I think DC is better than Marvel <span class="whatever">...` 

我需要删除每一个<...>所以它变成test = "I think DC is better than Marvel..."

我得到的最接近的是:

var str = "<hello> Test </hello>" 

str.replace(/<[^>]*/g, ""); //=> "> Test >"

str.replace(/<*>*/g, ""); //=> "hello Test /hello"

不完全是,虽然。我怎样才能做到这一点?

回答:

将HTML添加为新元素的innerHTML,然后提取textContent

let test = `<figure><img alt="" src="some_image.jpg"><figcaption>I think DC is better than Marvel <span class="whatever">`  

const el = document.createElement('div');

el.innerHTML = test;

const txt = el.textContent;

console.log(txt);

以上是 如何删除所有尖括号和使用JS内的一切? 的全部内容, 来源链接: utcz.com/qa/258288.html

回到顶部