Jump to content

RegEx - Turn Plain Text Into Links..?

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;

Edited by cyfer65
Link to post
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 post
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 post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...