2017-08-17 155 views
-1

我有文字正则表达式忽略HTML标签

<p>This is some text 
    <span style="color: rgb(54, 54, 54);"> and here is a bit more</span> 
    and even more. 
</p> 

和这个正则表达式<[^>]*>的字符串将抓住从它下面。

<p> 
    <span style="color: rgb(54, 54, 54);"></span> 
</p> 

我该如何获得相反的结果呢?我期待着取代文字。一直在搜索,只能设法找到使用上述正则表达式来替换去除标签的内容。

我正在寻找我的VueJS应用程序中的文本。

+2

https://stackoverflow.com/questions/6520192/get-text-node-of-an-element – Amy

+1

https://stackoverflow.com/a/44955924/392102 –

回答

1

首先,如果你可以访问DOM使用nodeElement.innerText 如果你不能那么HTML可能会是一个字符串,所以只需使用你的正则表达式,并用空字符串取代,以获得相反的。

console.log('1) ',target.innerText); 
 
console.log('2) ',target.innerHTML.replace(/<[^>]*>/g,''));
<div id="target"><p>This is some text<span style="color: rgb(54, 54, 54);"> and here is a bit more</span>and even more.</p></div>