This searches your entire page and replaces all numbers. You probably want to specify a tighter context to search in than the entire body and you may want to tweak the regex so that it matches specific patterns of numbers. Right now it matches any number in your page or sequences of integers separated by hyphens.
// 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)
})
);
});
Update: Here is a version that matches numbers in this pattern 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)
})
);
});