Begin Web Programming with PHP & MySQL

Advertisements



JavaScript RegExp Object

The RegExp object is used for matching a pattern of characters with a string. We use pattern matching for validating email, phone numbers, names and other regular patterns. Regular expressions can be used to perform all types of text search and text replace operations.

There are two ways of creating a new RegExp object:
Using the literal syntax
Regular expression consists of a pattern enclosed between slashes /.
Eg:
/abc/
Using the RegExp() constructor
We can create a regular expression by calling the RegExp() constructor function.
Eg:
var expression = new RegExp('abc');

., *, ?, +, [ ], ( ), { }, ^, $, | and \ are special characters which are used for special purposes in RegExp. If we want to use the above characters literally, we need to use a ‘\’ before the characters.

Properties of RegExp Object
Property Description
constructor It returns the function that created the RegExp object's prototype
Global (g) For checking the regular expression against all possible matches in a string, or only against the first.
ignoreCase (i) It defines whether the case is to be ignored during pattern matching in a string or not.
input The string against which a regular expression is matched.
lastIndex It defines the index to start the next match.
lastMatch The last matched characters.
multiline (m) It is used to define whether the search in strings should be performed across multiple lines.
prototype It is used to add new properties and methods to all instances of a class.
Methods of RegExp Object
Method Description
exec() It checks for a match in a string and returns the first match
test() It checks for a match in a string and returns the first match
toString() It returns the string value of the regular expression

Other than the above methods, we can use string methods match(), search(), replace() and split().

Regular Expression Flags

A flag is an optional parameter to a RegExp that defines its behaviour of searching such as global search and case-insensitive search. They can be used in single or as a combination.

Flags Use
g It performs a global match. The expression searches for all the occurrences.
i It makes the expression search case-insensitively.
m It performs multiline matches.
s It allows '.' to match newline characters.

Eg:
<script>
var string = "Hello, how are you?";
var search_pattern = /how/;
var search_result = string.search(search_pattern);
var search_result2 = string.match(search_pattern);
document.write("String: "+string+"<br> Search pattern: "+search_pattern+"<br><br>Using search method: "+search_result+"<br> Using match method: "+search_result2);
</script>
Output:
String: Hello, how are you?
Search pattern: /how/

Using search method: 7
Using match method: how

Character Classes

A character class is a special notation in brackets that matches any symbol from a predefined set of characters.

RegExp Description
[abc] It matches any one of the characters a, b or c.
[^abc] It matches any one character other than a, b or c.
[a-z] It matches any one character from lowercase a to lowercase z.
[A-Z] It matches any one character from uppercase a to uppercase z.
[a-Z] It matches any one character from lowercase a to uppercase Z.
[0-9] It matches a single digit between 0 and 9.
[a-z0-9] It matches a single character between a and z or between 0 and 9.

Eg:
<script>
var string = "a(k)*gt-69mNs_p-+-36G";
var regExp = /[a-zA-Z]/g; /* selects all the alphabets only*/
var allLetters = string.match(regExp);
document.write(allLetters);
</script>
Output:
a,k,g,t,m,N,s,p,G

Predefined Character Classes

Some character classes such as letters, digits, and whitespaces are used frequently in regular expression matching. For simplicity, predefined shortcuts have been given to such character classes.

Shortcut Description
. It matches any single character except newline \n.
\d matches any digit character. Same as [0-9]
\D Matches any non-digit character. Same as [^0-9]
\s Matches any whitespace character (space, tab, newline or carriage return character). Same as [ \t\n\r]
\S Matches any non-whitespace character. Same as [^ \t\n\r]
\w Matches any word character (defined as a to z, A to Z,0 to 9, and the underscore). Same as [a-zA-Z_0-9]
\W Matches any non-word character. Same as [^a-zA-Z_0-9]
Quantifiers

With quantifiers, we can specify how many times a character in a regular expression should match.

RegExp Description
n+ It matches one or more occurrences of the letter ‘n’.
n* It matches zero or more occurrences of the letter ‘n’.
n? It matches zero or one occurrence of ‘n’ in a string.
n{x} It matches ‘x’ occurrences of ‘n’s in a string.
n{x,y} It matches at least ‘x’ occurrences of ‘n’s, but not more than ‘y’ occurrences in a string.
n{x,} It matches ‘x’ or more occurrences of ‘n’s in a string.
n{,x} It matches at most ‘x’ occurrences of ‘n’s in a string.
^n It matches the letter ‘n’ at the beginning of a string.
n$ It matches the letter ‘n’ at the end of a string.

Eg:
<script>
var string = "Price is what you pay. Value is what you get.";
var regExp = /yo+/g; /* Check for one or more occurrences of ‘yo’ */
var allItems = string.match(regExp);
document.write(allItems);
</script>
Output:
yo,yo

Alternation (OR)

Alternation in regular expression resembles the OR (||) operation in conditional statements. You can specify alternation using a single vertical bar (|).

Eg:
<script>
var string = "Begin Web Programming Using PHP and MySQL";
var regExp = /php|mysql/gi; /* case insensitive matching for the string */
var allItems = string.match(regExp);
document.write(allItems);
</script>
Output:
PHP,MySQL