Daniel Rench

Web application development : Servers : Networks : E-Mail : DNS : Databases : Programming for hire

previous : contact : linkedin : code : links : pictures : facebook : twitter : next

A workaround for lacking textarea "SAR" technologies

Given the browser-as-wordprocessor hype, with your collaboration and flash embed capability and other "features," what happened to search and replace?

I wrote a small bookmarklet that finds all <textarea> elements in the current document and inserts 3 anonymous new elements:

  1. a "search" box that takes a regular expression,
  2. a "replace" box that takes a replacement string (with optional $1, etc.), and
  3. a button to execute the "SAR"
Don't worry about the extra elements; they have no names so they have no effect on the form at submit time.

Here it is: Search-N-Replace

And here's a textarea to try it on:

The unobfuscated source:

var D=document;
var tas=D.getElementsByTagName('textarea');
var i=tas.length;
while (i > 0) {
 (function(ta) {
  var f=ta.parentNode;
  var s=D.createElement('input');
  var r=D.createElement('input');
  var b=D.createElement('input');
  b.value='replace';
  s.type=r.type='text';
  b.type='button';
  b.onclick=function(){
   ta.value=ta.value.replace(new RegExp(s.value,'g'),r.value);
  };
  f.insertBefore(s,ta);
  f.insertBefore(r,ta);
  f.insertBefore(b,ta);
 })(tas[--i]);
}

Update: At least one of these has search & replace now. "I apologize for any difficulties this may have caused you."

<< The Lamest Javascript Compression Method? | Home | Portfolio | Contact | PongBounce (#2 in the Gratuitous Javascript Series) >>