我有各种文本区域:
<textarea><a href="/"><img src="/image1.gif" /></a></textarea>
<textarea><a href="/"><img src="/image2.gif" /></a></textarea>
<textarea><a href="/"><img src="/image2.gif" /></a></textarea>
使用 jQuery,我试图用正则表达式替换 href 和 src 值(必须保留 src 初始值),并将其转换为:
<textarea><a href="http://www.sitename.com"><img src="http://www.sitename.com/image1.gif" /></a></textarea> // see how we preserve the original src value by just adding the sitename
<textarea><a href="http://www.sitename.com"><img src="http://www.sitename.com/image2.gif" /></a></textarea>
<textarea><a href="http://www.sitename.com"><img src="http://www.sitename.com/image2.gif" /></a></textarea>
我发现使用以下正则表达式(?<=href=(\"|'))[^\"']+(?=(\"|'))
,(?<=src=(\"|'))[^\"']+(?=(\"|'))
我可以找到这些值,但其余的不知道......
到目前为止,这是我的代码:
$(function(){
(function(){
$('textarea').each(function(){
var obj = $(this);
obj.text().replace(REGEXP,"http://www.sitename.com/); //by placing the above regexp the script wont work
//missing the src part
});
})();
});
任何帮助表示赞赏