cyfer65 Posted September 7, 2011 Share Posted September 7, 2011 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 More sharing options...
ApocalypeX Posted September 7, 2011 Share Posted September 7, 2011 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! Link to comment Share on other sites More sharing options...
cyfer65 Posted September 7, 2011 Author Share Posted September 7, 2011 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 More sharing options...
ApocalypeX Posted September 7, 2011 Share Posted September 7, 2011 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 More sharing options...
cyfer65 Posted September 7, 2011 Author Share Posted September 7, 2011 this seemed to do the trick document.body.innerHTML = document.body.innerHTML.replace so how could I do the exact same thing except for IP Addresses..?they appear like this in the HTML Source Code. ___1.1.1.1</td> Link to comment Share on other sites More sharing options...
Ingolme Posted September 7, 2011 Share Posted September 7, 2011 This should work: /___(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/ That's four numbers between one and three digits, separated by dots. Link to comment Share on other sites More sharing options...
cyfer65 Posted September 7, 2011 Author Share Posted September 7, 2011 sweet thanks man.. works like a charm! Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.