8 August 2010

Chomp

I'm working on a website for a friend and I needed to chomp. So I worked out the regex.

Here you go.

Javascript:

EDIT: corrected!

// chomp
function chomp(text)
{
   // Check for empty
   if (text.search(/\S/) === -1)
   {
      return "";
   }
   else
   {
      // Chomp it.
      return text.replace(/^\s*((\S+\s+)*?\S+)\s*$/, "$1");
   }
}

No comments:

Post a Comment