问题描述
我需要除去HTML标记(包含类“ classmark”的“ a”标记除外)的正则表达式
让我说我有这个HTML字符串:
<b>this</b>
<a href="#">not match</a>
<a href="#" target="_blank">not match</a>
<a href="#" class="classmark" target="_blank">match</a>
<a href="#" class="classmark">match2</a>
<a class="classmark" target="_blank">match3</a>
<a class="classmark">match4</a>
<b>this</b>
<p>fggfgf</p>
我想要这样的结果:
this
not match
not match
<a href="#" class="classmark" target="_blank">match</a>
<a href="#" class="classmark">match2</a>
<a class="classmark" target="_blank">match3</a>
<a class="classmark">match4</a>
this
fggfgf
我使用此功能剥离HTML标签
function strip_tags( _html /*you can put each single tag per argument*/ )
{
var _tags = [],_tag = "" ;
for( var _a = 1 ; _a < arguments.length ; _a++ )
{
_tag = arguments[_a].replace( /<|>/g,'' ).trim() ;
if ( arguments[_a].length > 0 ) _tags.push( _tag,"/"+_tag );
}
if ( !( typeof _html == "string" ) && !( _html instanceof String ) ) return "" ;
else if ( _tags.length == 0 )
{
return _html.replace( /<(\s*\/?)[^>]+>/g,"" );
}
else
{
var _re = new RegExp( "<(?!("+_tags.join("|")+")\s*\/?)[^>]+>","g" );
return _html.replace( _re,'');
}
}
它将剥离HTML标记,并仅保留我想要相同功能的特定标记,并添加我需要类似以下内容的class属性:
strip_tags( HTMLstring,"a","classmark")
解决方法
如果我正确理解,则可以使用正则表达式来测试html是否包含带有类属性 x
的标记y
,然后可以使用{ {1}}个电话。可能是这样的:
.replace(regex,...)
编辑:
好吧,被误解了,并认为它是单个html标签的数组。因此,此版本首先将它们拆分为匹配的html标签(请注意,此版本将不嵌套标签),然后映射到所有部分并按部分进行替换。然后将它们重新加入:
[removed]
编辑:
如果您想要一个合适的HTML解析器并遍历每个元素,那么可以看看function strip_tags(_html,_tag,_class) {
return _html
// Match each tag and return them as an array of matches
.match(/<(.+).*?>.*?<\/\1>(.*?)([^<]*)/g)
// Map over each tag and check if it is a specific tag with a specific class
.map(tag => {
const regex = RegExp(`<${_tag} (.*?)class="${_class}"(.*?)>`);
// If it is,replace the tag part within nothing,and leave the content
if (!regex.test(tag)) {
return tag.replace(/(<([^>]+)>)/gi,'');
// If not then just return the tag as is
} else {
return tag;
}
})
// Now join all the mapped tags back together
.join('');
}
和this link to start with
DOMParser