JavaScript - 检查 html 字符串是否只有空标签或

Kir*_*oss -1 html javascript regex

我正在映射一个事件日历,其中一个 json 节点是艺术家简介。

对于生物节点,我将得到类似的东西并将其附加到生物分区。

<p>John Doe was born in Nantucket..</p>
<p>Yada yada</p>
Run Code Online (Sandbox Code Playgroud)

然而,有时我会得到这样的东西,我想将其设置为 null,这样它就不会打印:

<p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p>
Run Code Online (Sandbox Code Playgroud)

如何检查字符串中是否有空标签和/或&nbsp;具体来说,即

if(event.bio //has only empty tags || only white space || '&nbsp;') { 
     bio = null; 
}
Run Code Online (Sandbox Code Playgroud)

ari*_*uod 5

你可以这样做:

const aux = document.createElement('div');
aux.innerHTML = yourString; //parses the html
const trimmedContent = aux.innerText.trim(); //get only the text and remove white space at the beginning and end
Run Code Online (Sandbox Code Playgroud)

然后你可以检查是否trimmedContent == ''知道它是否为空。