RegExp忽略HTML标记

时间:2017-08-17 17:17:57

标签: javascript regex vue.js

我有一串文字

<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应用中搜索文字。

1 个答案:

答案 0 :(得分:1)

首先,如果你可以访问dom,请使用nodeElement.innerText 如果你不能,那么html可能是一个字符串,所以只需使用你的正则表达式并替换为空字符串以得到它的反转。

&#13;
&#13;
console.log('1) ',target.innerText);
console.log('2) ',target.innerHTML.replace(/<[^>]*>/g,''));
&#13;
<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>
&#13;
&#13;
&#13;