Jump to content

RegEx - Turn Plain Text Into Links..?


cyfer65

Recommended Posts

Im trying to make a greasemonkey scrip that turns all 4 digit numbers in plain text into a link on a site.I need some help with regex to find and replace all instances of plain text like this "___1234___" into links like<a href="http://site.com?ID=1234">1234</a> something like this, but not sure what the proper way to format it is to replace all the instances it finds..?var pattern = /___([0-9]{4})___/g;

Link to comment
Share on other sites

string.replace(regexp/substr,newstring) Will replace every instance it finds if you use a /g flag which you have. So just go str.replace(/___([0-9]{4})___/g, "<a href=\"http://site.com?ID=$1\">$1</a>"). Good luck!
Thanks, so how do I get it to replace the text on the webpage though?arn't I suppose to get the whole html into a variable or something to search through..what is the str representing..?
Link to comment
Share on other sites

It would help if you could say what the site is (I used to write GM scripts for 2 years). I'm guessing you're taking post id's? Well basically you take the element e.g. <p id="element">___1234___</p> var str = document.getElementById('element');str.innerHTML = str.innerHTML.replace(...);

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...