cyfer65 2 Posted September 7, 2011 Report Share Posted September 7, 2011 (edited) 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; Edited September 7, 2011 by cyfer65 Quote Link to post Share on other sites
ApocalypeX 3 Posted September 7, 2011 Report 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! Quote Link to post Share on other sites
cyfer65 2 Posted September 7, 2011 Author Report 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..? Quote Link to post Share on other sites
ApocalypeX 3 Posted September 7, 2011 Report 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(...); Quote Link to post Share on other sites
cyfer65 2 Posted September 7, 2011 Author Report 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> Quote Link to post Share on other sites
Ingolme 1,032 Posted September 7, 2011 Report 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. 1 Quote Link to post Share on other sites
cyfer65 2 Posted September 7, 2011 Author Report Share Posted September 7, 2011 sweet thanks man.. works like a charm! Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.