15 - Regular Expressions in Javascript

With numbers one can perform arithmetic operations. With string, one can perform different manipulative operations. Likewise one can also easily match the strings against different patterns. Regular expressions come into place here. They are patterns that are taken to be matched against strings. There are two types of expressions, those that are simple and those that are complex.

Simple regular expressions

A simple regular expression comprises of characters and not symbols of any kind. The characters are matched against the string and the substring is searched for to be found.

Regular Expression : sty
He was thirsty. => Has a match
This is his last year =>Does not have a match since a space comes between st and y.


Complex regular expressions

The simple regular expressions were direct characters that had to be matched. They do not comprise symbols that would convey a different meaning. Complex expressions are meant to search for more general patterns and more matches. They would have symbols apart from characters. These symbols would lend a special meaning to the expressions and would indeed help matching expressions and patterns.

CharacterExpressionExample with illustration
*The character or group of characters preceding the asterisk would occur zero or more times.Expression:ap*
The pattern means that there will be a match when a is followed by zero or more ps.
String:This is about her.
Result: Match exists.
Reasoning: a occurs in the string but no b.
String: The apple is sweet.
Result: Match exists.
Reasoning:a is followed by two ps.
String:The world is big.
Result:Does not match.
Reasoning: There is no a followed by b.
+The character or the group of characters preceding the plus would occur one or more times.Expression: b+
This pattern means that b should occur one or more times.
String: I am a girl
Result: Does not match
Reasoning: There is no b in the string.
String: Be Good to all.
Result: Match exists.
Reasoning: One B occurs in the string.
String: Here is the abbreviation.
?The character preceding the question mark occurs zero or one time.Expression:g?
This pattern means that either g does not occur or occurs once.
String: Good boy
Result: Match exists
Reasoning: One G occurs in the string.
String: Free flow of water
Result: Match exists
Reasoning: No G occurs in the string.
.The dot matches one character which can be letter, digit or symbol except for the new line character.Expression: .s
This pattern would match any character followed by an s.
String: Do as he says
Result: Match exists.
Reasoning: There are three matches in the string. The first is ‘as’. The second is ‘ s’. The third is ‘ys’.
String: Very big compartment.
Result: Match does not exist.
Reasoning: There is no s in the string.
[x]The match for the character or any one of the character within the square brackets is matched for.Expression: [ardt]
The pattern would need the occurrence of either a or r or d or t.
String: Wasp
Result: Match exists
Reasoning: a exists in the string.
String: Crop
Result: Match exists
Reasoning: r exists in the string.
String: dent
Result: Match exists
Reasoning: d exists in the string.
 String: toll
Result: Match exists
Reasoning: t exists in the string.
String: nose
Result: Match does not exist
Reasoning: There is no a or r or d or t in the string.
[^x]The characters occurring within the brackets should not occur.Expression: [^ge]
This pattern means that the string should not contain a g or an e.
String: Hurray
Result: Match exists
Reasoning: There is no g or e in the string.
x|yThe match for either x or y is looked for.Expression: f|h
The pattern means that the string will match if it has an f or a h.
String: You are free.
Result: Match exists.
Reasoning: There is an f.
String: Hurray!
Result: Match exists.
Reasoning: There is a h.
String: Alas
Result: No match exists
Reasoning: There is no f or h.
{number of occurences}The character should occur for the number of times specified.Expression: p{2}
The pattern means that p should occur twice.
String: Helicopter
Result: Match does not exist.
Reasoning: There is only one p.
String: Application
Result: Match exists
Reasoning: There are two ps.
{at least number of occurrences, at most number of occurrences}The character should occur minimum number of times as per the first argument and maximum the second argument number of times.Expression: f{2,3}
The pattern means that f should occur at least twice and not more than thrice.
String: Difficult
Result: Match exists
Reasoning: There are two fs.
String: Just a jiffffy
Result: Match exists
Reasoning: There are three fs. The pattern matches only the first three.
\The backslash makes the character that comes after it non special. This means that the character following the slash must be taken literally and not interpreted specially.Expression 1: a*
Meaning: This expression means that a should occur zero or more times in the string.
Expression 2: a\*
Meaning: The backslash de specialises the * and the match is taken for an a followed by *.
 
^This matches the pattern at the beginning of the string.Expression: ^E
The pattern means that e should occur at the beginning of the string.
String: East is bright
Result: Match exists
Reasoning: An E occurs at the beginning.
String: West is dark
Result: Match does not exist
Reasoning: The e does not occur in the beginning of the string.
$This matches the pattern against the end of the string.Expression: r$
The pattern means that the input should end with r.
  String: Hurray
Result: Match does not exist
Reasoning: The r does not occur at the end of the string.
  String: It is Easter
Result: Match exists
Reasoning: The r occurs at the end of the string.

RegExp Object

In Javascript regular expressions can be described using the RegExp object. It is created with the pattern and any modifiers if needed. A modifier imposes certain restrictions on how the patterns should be matched. Given below are some modifiers.

ModifierPurpose
mThis modifier enables searching along multilines.
gThis modifier enables search for the entire page rather than stopping after the first match is found.
iThis modifier enables non case sensitive match.

Creating a regular expression

var pattern = new RegExp(/pattern/, modifiers);
    (or)
var pattern = /pattern/modifiers;


Meta characters – characters that have a special meaning

There are some patterns that have already been established with a meaning. Here are some.

Meta character

Purpose

\w

This meta character finds a word character.

\W

This meta character finds a non word character.

\d

This meta character finds a character that is a digit.

\D

This meta character finds a character that is not a digit.

\s

This meta character finds a character that is a whitespace.

\S

This meta character finds a character that is not a white space.

\0

This meta character finds a null.

\n

This meta character finds a new line character.

\f

This meta character finds a form feed.

\r

This meta character finds a carriage return character.

\t

This meta character finds a tab space

 

exec() function

This function is used to find a match for a pattern in a string. It will return the first match it finds.

<!DOCTYPE html>
<html>
    <head>
        <script type="text/javascript">
        </script>
    </head>
    <body style = "font-size:16px;">
       <hr>
       <script type="text/javascript">
            var stringTest = "This is a test string";
            var firstPattern = /st/;
            document.write("Here is the result for /st/ " + firstPattern.exec(stringTest) + “.<br/>”);
            var secondPattern = /so/;
            document.write("Here is the result for /so/ " + secondPattern.exec(stringTest)); 
        </script>
    </body>
</html>


test() function

The test() function is used to check whether the regular expression pattern matches to the string. It returns a true or false based on the evaluation.

<!DOCTYPE html>
<html>
    <head>
        <script type="text/javascript">
        </script>
    </head>    
    <body style = "font-size:16px;">
       <hr>
        <script type="text/javascript">
            var stringTest = "This is a test string";
            var firstPattern = /st/;
            document.write("Here is the result for /st/ " + firstPattern.test(stringTest) + ".<br/>");
            var secondPattern = /so/;
            document.write("Here is the result for /so/ " + secondPattern.test(stringTest)); 
        </script>
    </body>
</html>


Like us on Facebook