因此,我在后端发出 api 请求时收到此字符串格式(这是来自 flash 的遗留代码,我们必须将其转换为 html):
<TextFlow whiteSpaceCollapse="preserve" version="2.0.0" xmlns="http://ns.adobe.com/textLayout/2008"><p><a href="http://yahoo.com"><span>yahoo</span></a><span> </span><a href="http://google.com"><span>google</span></a></p></TextFlow>
在客户端,我被要求删除除 ahref 标签之外的 html 标签并显示它们。所以预期的结果是:
<a href="http://yahoo.com"><span>yahoo</span></a>
<a href="http://google.com"><span>google</span></a>
到目前为止我所做的是:
var htmlString = this.model.get( 'FolderDescription' );
htmlString.replace(/href="([^\'\"]+)/g, function( match ) {
matches.push( match );
} )
我得到的输出是:
["href="http://yahoo.com", "href="http://google.com"]
如何显示 ahref 标签(包括标签内的文本)?
最佳答案
你就快到了。只需将所有字符匹配到下一个结束 anchor 标记即可。
<a\b[^<>]*\bhref="[^\'\"]+".*?<\/a>
> var s = '<TextFlow whiteSpaceCollapse="preserve" version="2.0.0" xmlns="http://ns.adobe.com/textLayout/2008"><p><a href="http://yahoo.com"><span>yahoo</span></a><span> </span><a href="http://google.com"><span>google</span></a></p></TextFlow>'
undefined
> s.match(/<a\b[^<>]*\bhref="([^\'\"]+)".*?<\/a>/g)
[ '<a href="http://yahoo.com"><span>yahoo</span></a>',
'<a href="http://google.com"><span>google</span></a>' ]
关于javascript - 通过正则表达式匹配 ahref 标签然后转换为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28535375/