我的文本内容大致如下:
<a href="http://example.com/test this now">Stuff</a>
More stuff
<a href="http://example.com/more?stuff goes here">more</a>
我希望用一个函数替换href
中的内容,使得只有URL部分被进行URL编码。
我该如何实现?
更新:这是我尝试过的:
postdata.comment.content = postdata.comment.content.replace(/href=\"(.+?)\"/g, function(match, p1) {
return encodeURI(p1);
});
没有达到我的期望。
期望的结果是:
<a href="http%3A%2F%2Fexample.com%2Ftest%20this%20now">Stuff</a>
More stuff
<a href="http%3A%2F%2Fexample.com%2Fmore%3Fstuff%20goes%20here">more</a>
$(pattern).attr('href', 'http://youpi.test/bidule');
- Pierre Emmanuel Lallemant