Code-Parsing Regular Expressions via Lighter.js

By  on  

Perfecting a regular expression can take a lot of time and testing but once achieved can be a absolutely golden. While looking through the source code of MooTools syntax highlighter Lighter.js I stumbled upon a few code-parsing regular expressions that you might be interested in.

The JavaScript

	// Matches a C style single-line comment.
	slashComments: /(?:^|[^\\])\/\/.*$/gm,
	
	// Matches a Perl style single-line comment.
	poundComments: /#.*$/gm,
	
	// Matches a C style multi-line comment.
	multiComments: /\/\*[\s\S]*?\*\//gm,
	
	// Matches a string enclosed by single quotes.
	aposStrings:   /'[^'\\]*(?:\\.[^'\\]*)*'/gm, 
	
	// Matches a string enclosed by double quotes.
	quotedStrings: /"[^"\\]*(?:\\.[^"\\]*)*"/gm, 
	
	// Matches both.
	strings:       /'[^'\\]*(?:\\.[^'\\]*)*'|"[^"\\]*(?:\\.[^"\\]*)*"/gm,
	
	// Matches a property: .property style.
	properties:    /\.([\w]+)\s*/gi,   
	
	// Matches a method call: .methodName() style.
	methodCalls:   /\.([\w]+)\s*\(/gm, 
	
	// Matches a function call: functionName() style.
	functionCalls: /\b([\w]+)\s*\(/gm,   
	
	// Matches any of the common brackets.
	brackets:      /\{|\}|\(|\)|\[|\]/g, 
	
	// Matches integers, decimals, hexadecimals.
	numbers:       /\b((?:(\d+)?\.)?[0-9]+|0x[0-9A-F]+)\b/gi 

Regular expressions can look heinous so I apologize to anyone whose brains imploded after looking at the above hieroglyphics text. Have useful regular expressions you use often? Share them!

Recent Features

  • By
    CSS @supports

    Feature detection via JavaScript is a client side best practice and for all the right reasons, but unfortunately that same functionality hasn't been available within CSS.  What we end up doing is repeating the same properties multiple times with each browser prefix.  Yuck.  Another thing we...

  • By
    Welcome to My New Office

    My first professional web development was at a small print shop where I sat in a windowless cubical all day. I suffered that boxed in environment for almost five years before I was able to find a remote job where I worked from home. The first...

Incredible Demos

  • By
    jQuery Random Link Color Animations

    We all know that we can set a link's :hover color, but what if we want to add a bit more dynamism and flair? jQuery allows you to not only animate to a specified color, but also allows you to animate to a random color. The...

  • By
    Google-Style Element Fading Using MooTools or jQuery

    Google recently introduced an interesting effect to their homepage: the top left and top right navigation items don't display until you move your mouse or leave the search term box. Why? I can only speculate that they want their homepage as...

Discussion

  1. David: very interesting, could you provide examples for each expression? ty so much!

  2. That’s pretty neat

    I’ve always used http://www.gskinner.com/RegExr/ to text my RegExp

  3. Well, im not a veteran programmer but i did a noobMatcher to test my regExp hehe

    http://vrs.host56.com/noobMatcher/

  4. Thanks for this Tutorial. Can u provide Demo for this.

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