cyfer65 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 Link to comment Share on other sites More sharing options...
ApocalypeX 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! Link to comment Share on other sites More sharing options...
cyfer65 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..? Link to comment Share on other sites More sharing options...
ApocalypeX 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(...); Link to comment Share on other sites More sharing options...
cyfer65 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> Link to comment Share on other sites More sharing options...
Ingolme 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 Link to comment Share on other sites More sharing options...
cyfer65 Posted September 7, 2011 Author Report Share Posted September 7, 2011 sweet thanks man.. works like a charm! Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now