String Regular Expressions with MooTools

By  on  

Whether you like given JavaScript library or not , there are always snippets you can take from the code which accomplish a task you may need to address.  We all know that I looooooove MooTools, but maybe you're a Dojo or jQuery developer that doesn't get a good look at Moo.  The following are a choice selection of regular expression used with MooTools -- maybe you have use for them?

Clean Whitespace

var newString = myString.replace(/^\s+|\s+$/g, '');

Camel-Case a Dash-Separated String

var newString = myString.replace(/-\D/g, function(match){
	return match.charAt(1).toUpperCase();
});

Dash-Separate a Camel-Cased String

var newString = myString.replace(/[A-Z]/g, function(match){
	return ('-' + match.charAt(0).toLowerCase());
});

Escape a Regular Expression

var newString = myString.replace(/([-.*+?^${}()|[\]\/\\])/g, '\\$1');

Hex to RGB, RGB to Hex

var hex = myString.match(/^#?(\w{1,2})(\w{1,2})(\w{1,2})$/);
var newString = (hex) ? hex.slice(1).hexToRgb(array) : null;

Term Substitution

var newString = this.replace(regexp || (/\\?\{([^{}]+)\}/g), function(match, name){
	if (match.charAt(0) == '\\') return match.slice(1);
	return (object[name] != undefined) ? object[name] : '';
});

Strip Scripts from a String

var newString = myString.replace(/<script[^>]*>([\s\S]*?)<\/script>/gi, function(all, code){
	scripts += code + '\n';
	return '';
});

Above are just a sampling of regular expression used by MooTools to accomplish tasks.  Most, in fact, are applied as methods to the String native so they are available with *ANY* string!

Recent Features

  • By
    CSS vs. JS Animation: Which is Faster?

    How is it possible that JavaScript-based animation has secretly always been as fast — or faster — than CSS transitions? And, how is it possible that Adobe and Google consistently release media-rich mobile sites that rival the performance of native apps? This article serves as a point-by-point...

  • By
    39 Shirts &#8211; Leaving Mozilla

    In 2001 I had just graduated from a small town high school and headed off to a small town college. I found myself in the quaint computer lab where the substandard computers featured two browsers: Internet Explorer and Mozilla. It was this lab where I fell...

Incredible Demos

  • By
    Jack Rugile&#8217;s Favorite CodePen Demos

    CodePen is an amazing source of inspiration for code and design. I am blown away every day by the demos users create. As you'll see below, I have an affinity toward things that move. It was difficult to narrow down my favorites, but here they are!

  • By
    Basic AJAX Requests Using MooTools 1.2

    AJAX has become a huge part of the modern web and that wont change in the foreseeable future. MooTools has made AJAX so simple that a rookie developer can get their dynamic pages working in no time. Step 1: The XHTML Here we define two links...

Discussion

  1. Jeffrey

    Technologies will come and go – one library will find favor over the one you are currently using, one scripting language will arise that will make the one you are using look foolish, etc. But regular expressions are one of those things that will be a useful thing to know even on the tumultuous seas of techno-change.

    Good little article – make it a series. A cache of tested regular expressions to crib from is a useful thing to have.

    • Hear, hear!

      Perl was the first programming language I ever learned, so Regular Expressions have always been a primary tool I’ve used no matter the language. I just don’t know what I’d do without them!

      ^_^

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!