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
    39 Shirts – 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...

  • By
    CSS Filters

    CSS filter support recently landed within WebKit nightlies. CSS filters provide a method for modifying the rendering of a basic DOM element, image, or video. CSS filters allow for blurring, warping, and modifying the color intensity of elements. Let's have...

Incredible Demos

  • By
    pointer Media Query

    As more devices emerge and differences in device interaction are implemented, the more important good CSS code will become.  In order to write good CSS, we need some indicator about device capabilities.  We've used CSS media queries thus far, with checks for max-width and pixel ratios.

  • By
    CSS Fixed Position Background Image

    Backgrounds have become an integral part of creating a web 2.0-esque website since gradients have become all the rage. If you think gradient backgrounds are too cliche, maybe a fixed position background would work for you? It does provide a neat inherent effect by...

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!