Read, With the Name of Your Lord Who Created

RegExp Javascript Object Methods

Posted by triaslama on February 25, 2008

This post is supplement for my previous post on Javascript Regex, I am not explore something here just write about RegExp methods and hope this will useful. Below the list of RegExp method and its descriptions:

1. test(<string>). Test whether a supplied string matched a specified pattern the return value is true or false. Example:

var re = new RegExp(“^tr*i+$”);
var result = re.test(window.prompt(“Input string: “));
window.alert(“Result: “+result);

2. exec(<string>). Checks whether the supplied string match a predefined regular expression of a specified Regex object. Return the match string that matched if the pattern was found in the supplied string, or null if no match pattern in supplied string. Example:

var re = new RegExp(“catch”);
var result = re.exec(“I’ll catch you later!”);
window.alert(“Result: “+result);

3. compile(newPattern). Change the pattern of an RegExp object, this method does not return a value. Example:

var re = new RegExp(“^tr*i+$”);
var result = re.test(“hello tri”);
window.alert(“result: “+result+”, pattern will be changed.”);
re.compile(“tr*i+$”);
result = re.test(“hello tri”);
window.alert(“result: “+result);

The behaviour of literal Regex and RegExp object in Regular expressions is a bit different, for simplicity I will choice literal Javascript Regex in most case, its up to you whether use literal syntax or RegExp object to handle regular expressions in Javascript.
Thanks and regards, Tri Sugiyantowo

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: