Difference in the use of Two methods of expression Regular Expressions



We has so far been using two methods of expressing the regular expression. We indicated that the following two are equivalent

var pattern1 = new RegExp("^[1-2]?[0-9]{1}$");
var pattern1 = /^[1-2]?[0-9]{1}$/;


There is however one important distinction. This is in using the backward slash. The backward slash indicates a special character in string. We know that \w indicates any word. To understand it, let us revisit the example


/*
********************************************************
Javascript Regular Expression Example17
Understanding use of word character \w
********************************************************
*/
var pattern1=/\w/;
var string1 = new Array(3);
string1[0] = "*&$#";
string1[1] = "michaeljacson";
string1[2] = "*&_$#";
var i;
for(i=0; i<=2; i++)
{
if (pattern1.test(string1[i]))
{
document.write(string1[i], " -> has word character","<br\>");
}
else
{
document.write(string1[i], " -> does not have word character","<br\>");
}
}



This example tries to find if the given strings contain “a” valid word. As obvious string[1] or “michaeljacson” does contain a valid word and therefore it gives the output - has word character. The string "*&_$#" gives output has word character because it has a underscore _ in it. This script has the following output.

*&$# -> does not have word character
michaeljacson -> has word character
*&_$# -> has word character


Now let us rewrite the same program using the RegExp() constructor method.

/*
********************************************************
Javascript Regular Expression Example 19
********************************************************
*/
var pattern1 = new RegExp("\w");
var string1 = new Array(3);
string1[0] = "*&$#";
string1[1] = "michaeljacson";
string1[2] = "*&_$#";
var i;
for(i=0; i<=2; i++)
{
if (pattern1.test(string1[i]))
{
document.write(string1[i], " -> has word character","<br\>");
}
else
{
document.write(string1[i], " -> does not have word character","<br\>");
}
}

Try this Example online


You can try this example online at - here .

This script gives the following output.

*&$# -> does not have word character
michaeljacson -> does not have word character
*&_$# -> does not have word character


If we however, modify the regular expression as

var pattern1 = new RegExp("\\w"); 


It will give the expected result. Here is the complete example

/*
********************************************************
Javascript Regular Expression Example 20
********************************************************
*/
var pattern1 = new RegExp("\\w");
var string1 = new Array(3);
string1[0] = "*&$#";
string1[1] = "michaeljacson";
string1[2] = "*&_$#";
var i;
for(i=0; i<=2; i++)
{
if (pattern1.test(string1[i]))
{
document.write(string1[i], " -> has word character","<br\>");
}
else
{
document.write(string1[i], " -> does not have word character","<br\>");
}
}

Try this Example online


You can try this example online at - here .

And the result is

*&$# -> does not have word character
michaeljacson -> has word character
*&_$# -> has word character


So what does the expression

var pattern1 = new RegExp("\w");

matches to ?

Let us again experiment and rewrite the above example.

/*
********************************************************
Javascript Regular Expression Example 21
********************************************************
*/
var pattern1 = new RegExp("\w");
var string1 = new Array(3);
string1[0] = "world";
string1[1] = "michael";
string1[2] = "World";
var i;
for(i=0; i<=2; i++)
{
if (pattern1.test(string1[i]))
{
document.write(string1[i], " -> has word character","<br\>");
}
else
{
document.write(string1[i], " -> does not have word character","<br\>");
}
}

Try this Example online


You can try this example online at - here .

It gives the following output

world -> has word character
michael -> does not have word character
World -> does not have word character
Obviously it simply matches the character w.