Это ищет всю вашу страницу и заменяет все номера. Возможно, вы захотите указать более узкий контекст для поиска, чем все тело, и вы можете настроить регулярное выражение так, чтобы оно соответствовало определенным шаблонам чисел. Прямо сейчас он соответствует любому числу на вашей странице или последовательности целых чисел, разделенных дефисами.
// This is a simple jQuery extension to find text nodes
$.fn.textNodes = function() {
var ret = [];
$.each(this.contents(), function() {
try {
if(this.nodeType == 3)
ret.push(this);
else
$(this).contents().each(arguments.callee);
} catch(e) {}
});
return $(ret);
}
// Get all the text nodes from the body
$(document.body).textNodes().each(function() {
// Test each text node to see if it has any numbers in it
if(/\d/.test(this.nodeValue))
// Remove numbers or sequences of numbers and replace with links
$(this).replaceWith(
this.nodeValue.replace(/\d+(?:-\d+)*/g, function(nums) {
return '<a href="http://www.mysite.com/page.php?ids=' + nums.split('-').join(',') + '">Path</a>'; // str.split(a).join(b) is faster than str.replace(a,b)
})
);
});
Обновление: Вот версия, которая соответствует числам в этом шаблоне xx-xx-xxx-xxxxxxxx
// Get all the text nodes from the body
$(document.body).textNodes().each(function() {
// Test each text node to see if it has our pattern of numbers in it
if(/\d{2}-\d{2}-\d{3}-\d{8}/.test(this.nodeValue))
// Remove sequences of numbers and replace with links
$(this).replaceWith(
this.nodeValue.replace(/\d{2}-\d{2}-\d{3}-\d{8}/g, function(nums) {
return '<a href="http://www.mysite.com/page.php?ids=' + nums.split('-').join(',') + '">Path</a>'; // str.split(a).join(b) is faster than str.replace(a,b)
})
);
});