所以我在后端发出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>
在客户端,我被要求删除html标签除了 ahref标签并显示它们。所以预期的结果是:
<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标签?
答案 0 :(得分:1)
你快到了。只需将所有字符匹配到下一个结束锚标记。
<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>' ]
答案 1 :(得分:1)
使用捕获组的正则表达式:
href="([\w\/.\:]*)"