Subscribe to Tutorial Feed

Flash and PHP Bible

The Flash and PHP Bible has been released! The book can be found on Amazon or wherever fine books are sold in your area.

The Flash and PHP Bible has a forum for quick support.

Scriptplayground » tutorials » js » Match content using Regular Expressions

Match content using Regular Expressions

Match content in Javascript using Regular Expressions.

The topic of regular expressions can be advanced, but this article will drill down to a specific use

The problem is, you have a link <a href="">Link Me</a> which you want to match just the "Link Me" portion.

<script type="text/javascript">

var haystack = '<a href="test">Link Me</a>';
var regexPattern = new RegExp(/^<.*>(.*)<\/.*>$/i);

var matches = new Array();
matches = haystack.match(regexPattern);

window.onload = function()
	matches.splice(0, 1);


As you can see, the meat of this code is within the new RegExp() call.

The actual matching pattern is:


This example is called once the page loads and after the match is completed, simply alerts (outputs) the matches in the form of a dialog box.

The "match()" function is called from the string and takes the regular expression pattern as an argument.

Once the pattern array is filled you want to remove the first entry, because it contains the complete matched pattern. This is achieved using splice() function on the newly created array.

matches.splice(0, 1);

That is all there is to this basic regular expression matching system

The next step will be to form this into a complete tool and even port it to ActionScript, now that AS3 offers native regular expression support.

Follow Scriptplayground on Twitter (@scriptplay)

Comments: Match content using Regular Expressions

No Comments have been made. Wanna be the first?
Add a comment
The ability to add new comments has been disabled in this archived version.

Main | Tutorials | Articles