此 jquery 语句将查找 domain.com 并将 ? 参数附加到 URL 的末尾。如果 ? 参数已经添加,它将不会附加。
问题:我当前的 jquery 修改了所有 URL 而不是 domain.com。这是我想使用并经过测试可以工作的正则表达式语句。但是,在实施时,没有附加任何内容。任何帮助是极大的赞赏!
我想使用的正则表达式:
\b(https?://)?([a-z0-9-]+\.)*domain\.com(/[^\s]*)?
JSFiddle为方便起见
待修改代码
<div id="wp-content-editor-container" class="wp-editor-container"><textarea class="wp-editor-area" rows="10" tabindex="1" cols="40" name="content" id="content"><a title="Link to test domain" href="http://www.domain.com">Link to google</a>
<a href="http://www.google.com/directory/subdirectory/index.html">This is another link</a>
<a href="http://domain.com/directory/index.html">This is a 3rd link</a>
<a href="http://www.domain.com/subdir?parameter">this url already has parameters</a></textarea></div>
当前的 jquery 语句
var url = 'www.domain.com';
var append = '?parameter';
$(".wp-editor-area").each(function() {
$(this).text(urlify($(this).text()));
});
function urlify(text) {
var urlRegex = /(\b(https?|ftp|file):\/\/[www.domain.com][-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
return text.replace(urlRegex, function(url) {
// if the url does not contain append, concat append to the URL
if (url.indexOf(append) == -1) {
return url + append;
}
return url;
});
}
电流输出
<a title="Link to test domain" href="http://www.domain.com?parameter">Link to google</a>
<a href="http://www.google.com/directory/subdirectory/index.html?parameter">This is another link</a>
<a href="http://domain.com/directory/index.html?parameter">This is a 3rd link</a>