Har*_*rry 0 javascript jquery replace
快速问题 - 为什么这不起作用?
我很确定我已经测试了一切都无济于事.我正在尝试基本上添加mailto链接到任何可以找到的电子邮件.
它不是用邮件替换电子邮件链接到标签.
谢谢,哈利
$(document).ready(function() {
var email_regex = /([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+)/gi;
var bodyText = $('body').html();
var match = email_regex.exec(bodyText);
// To do - Don't try it when there is already a mailto link, can probably just add mailto to the regex.
for (var i = 0; i < match.length; i++) {
bodyText.replace(match[i], '<a href="mailto:' + match[i] + '">' + match[i] + '</a>');
console.log(match[i]);
}
$('body').html(bodyText);
console.dir(match);
});
Run Code Online (Sandbox Code Playgroud)
我应该改为:
var result = bodyText.replace(email_regex,'<a href="mailto:$1">$1</a>');
console.log(result); // This two lines are enough.
Run Code Online (Sandbox Code Playgroud)
完整代码:
$(document).ready(function() {
var email_regex = /([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+)/gi;
var bodyText = $('body').html();
var result = bodyText.replace(email_regex,'<a href="mailto:$1">$1</a>');
console.log(result); // This two lines are enough.
});
Run Code Online (Sandbox Code Playgroud)