Tuesday, September 21, 2010

Regular Expression Search Bookmarklet

iPhone BookmarkletsI have an old website where I keep most of my bookmarklets. I'm planning on deprecating that site and just putting up some personal stuff (since I don't do what that site says I do anymore).

This bookmarklet is probably my most used bookmarklet. Basically, you enter a regular expression and each match in the page will be highlighted. It cycles through 16 color schemes to change the highlight color.

If you just want to install the bookmarklet, grab this link and drag it onto your bookmarklet toolbar in your browser.
Regex Search

If you want to use it on your mobile device, you can use my Mobile Bookmarklet Installer Bookmarklet (which, by the way, will install itself too).

I'm sure it doesn't work in IE, but I haven't tested it in a really long time. If you'd like to see what it would do in IE if IE didn't suck so badly, just click it.

If you're interested in the code, here it is!
  1. // check to see if the variable searches has been defined.  
  2. // if not, create it.  this variable is to cycle through   
  3. // highlight colors.  
  4. if (typeof(searches) == 'undefined')  
  5. {  
  6.   var searches = 0;  
  7. };  
  8.   
  9. (  
  10.   function()  
  11.   {  
  12.     // just some variables  
  13.     var count = 0, text, regexp;  
  14.   
  15.     // prompt for the regex to search for  
  16.     text = prompt('Search regexp:''');  
  17.   
  18.     // if no text entered, exit bookmarklet  
  19.     if (text == null || text.length == 0)  
  20.       return;  
  21.   
  22.     // try to create the regex object.  if it fails  
  23.     // just exit the bookmarklet and explain why.  
  24.     try  
  25.     {  
  26.       regexp = new RegExp(text, 'i');  
  27.     }  
  28.   
  29.     catch (er)  
  30.     {  
  31.       alert('Unable to create regular expression using text \'' + text + '\'.\n\n' + er);  
  32.       return;  
  33.     }  
  34.   
  35.     // this is the function that does the searching.  
  36.     function searchWithinNode(node, re)  
  37.     {  
  38.       // more variables  
  39.       var pos, skip, acronym, middlebit, endbit, middleclone;  
  40.       skip = 0;  
  41.   
  42.       // be sure the target node is a text node  
  43.       if (node.nodeType == 3)  
  44.       {  
  45.         // find the position of the first match  
  46.         pos = node.data.search(re);  
  47.   
  48.         // if there's a match . . .   
  49.         if (pos >= 0)  
  50.         {  
  51.           // create the acronym node.  
  52.           acronym = document.createElement('ACRONYM');  
  53.           acronym.title = 'Search ' + (searches + 1) + ': ' + re.toString();  
  54.           acronym.style.backgroundColor = backColor;  
  55.           acronym.style.borderTop = '1px solid ' + borderColor;  
  56.           acronym.style.borderBottom = '1px solid ' + borderColor;  
  57.           acronym.style.fontWeight = 'bold';  
  58.           acronym.style.color = borderColor;  
  59.       
  60.     // get the last half of the node and cut the match  
  61.     // out.  then, clone the middle part and replace it with  
  62.     // the acronym  
  63.           middlebit = node.splitText(pos);  
  64.           endbit = middlebit.splitText(RegExp.lastMatch.length);  
  65.           middleclone = middlebit.cloneNode(true);  
  66.           acronym.appendChild(middleclone);  
  67.           middlebit.parentNode.replaceChild(acronym, middlebit);  
  68.           count++;  
  69.           skip = 1;  
  70.         }  
  71.       }  
  72.   
  73.       // if the node is not a text node and is not  
  74.       // a script or a style tag then search the children  
  75.       else if (  
  76.         node.nodeType == 1  
  77.         && node.childNodes  
  78.         && node.tagName.toUpperCase() != 'SCRIPT'  
  79.         && node.tagName.toUpperCase != 'STYLE'  
  80.       )  
  81.         for (var child = 0; child < node.childNodes.length; ++child)  
  82.           child = child + searchWithinNode(node.childNodes[child], re);  
  83.   
  84.       return skip;  
  85.     }  
  86.   
  87.     // use the search count to get the colors.  
  88.     var borderColor = '#'   
  89.       + (searches + 8).toString(2).substr(-3)  
  90.       .replace(/0/g, '3')  
  91.       .replace(/1/g, '6');  
  92.       
  93.     var backColor = borderColor  
  94.       .replace(/3/g, 'c')  
  95.       .replace(/6/g, 'f');  
  96.   
  97.     // for the last half of every 16 searhes, invert the  
  98.     // colors.  this just adds more variation between  
  99.     // searches.  
  100.     if (searches % 16 / 8 >= 1)  
  101.     {  
  102.       var tempColor = borderColor;  
  103.       borderColor = backColor;  
  104.       backColor = tempColor;  
  105.     }  
  106.   
  107.     searchWithinNode(document.body, regexp);  
  108.     window.status = 'Found ' + count + ' match'  
  109.       + (count == 1 ? '' : 'es')  
  110.       + ' for ' + regexp + '.';  
  111.   
  112.     // if we made any matches, increment the search count  
  113.     if (count > 0)  
  114.       searches++;  
  115.   }  
  116. )();  

16 comments:

  1. Well, This is an interesting software which is like by many people and some of the people would like to learn more from here. I really enjoy the information you have posted here. Thank you so much for posting this article. free download software for pc

    ReplyDelete
  2. This article has many new features which shares many valuable things. Thank you so much for posting this article. full version

    ReplyDelete
  3. Well, what is the main purpose of this software. please explain here. free download software for pc

    ReplyDelete
  4. JavaScript Shell Code is amazing ...... Thanks for this code...
    FREE DOWNLOAD SOFTWARE | FULL VERSION

    ReplyDelete
  5. The great post about JavaScript, thanks a lot for sharing.
    full version software blogspot sites

    ReplyDelete
  6. Good sharing! thanks for knowledgeable posting for bookmarklets.
    PC Software Download

    ReplyDelete
  7. valuable post! very interesting and helpful sharing!
    Software Crack | Crack Software Download

    ReplyDelete
  8. I really enjoy the information you have shared. Thank you so much..!!
    Software Crack | Free Download Software

    ReplyDelete
  9. This is one of the amazing information. thanks a lot for sharing...!!!!
    Software Full Version | Download Free Software

    ReplyDelete
  10. Subscribe me -> http://ggmalgues.890m.com/

    ReplyDelete
  11. Had to remove all the comments from the script and prefix it with "javascript:" but now it's working excellently. Powerful and impressive little bookmarklet.

    ReplyDelete