Header Ads Widget

What is regular expression in JavaScript?

When you interface with website and input data one thing to notice that some information input allowed in specific pattern only.

For example, In email or URL field you can enter email address like abcword@yahoo.com or www.blogspot.com. In URL @,dot(.) and domain name etc.. are required in particular order. If you miss these characters or mismatch its order then you will get error. In email or URL this type of pattern is defined by regular expression.

This post will describe about regular expression, their use , RegExp object method and properties.

Let’s starts with definition.


What is regular expression?

Regular expression is a search pattern made with sequence of characters matches with string. A regular expression can be single character or simple to more complex pattern.

Usage of regular expressions

Regular expressions are mainly used to find and replace string. Regular expressions are used to filter data.

How to write regular expression?

Syntax

Regular expression is written in between forward slashes.

/Regular expression pattern/;

Modifiers added after slash.

/Pattern/modifiers;

Example

This is simple pattern of regular expression contains a word “Hello”.

var s= /Hello/;


What the regular expression includes?

The regular expression includes modifiers, quantifiers, Brackets and Metacharacters.

Modifiers/ flags

Modifiers/flags are written at end and multiple modifiers can be used.

Modifiers/flagsDescription
iMatches characters with case-insensitivity.
gMatches globally (not stop after first match).
sMatches characters starts with new line.
mMatches multiline characters.

Quantifiers

Quantifiers are the symbols defines quantity of matching characters. Write string or characters you want to match before quantifiers.


QuantifiersDescription
+Selects one or more matching characters.
*Selects 0 or more than 0 matches.
?Selects characters which may or may not present in string.
{}It matches characters numbers of specified value in bracket.
^Selects characters that starting at new line or string.
$Selects characters at end of line or string.

Metacharacters

Metacharacters have special meaning and matches set of characters.


MetacharactersDescription
\dMatches only digits.
\sMatches white space characters (like tab, new line, and space)
\bMatches characters at beginning or at end of the string.
\DMatches non-digit characters.
\SMatches non-white space characters.

Brackets

Selects characters according to value specified in [] brackets.


BracketsDescription
[0-9]Selects only numeric characters.
[a-z]Selects lower case alphabets only.
[A-Z]Selects upper case characters.
[characters]Selects any characters present in bracket.
[^characters]Selects characters that is not in bracket.
(characters|characters)Selects any of characters specified in bracket (). Both are seperated by |.

Regular expression in JavaScript

In JavaScript regular expression is object. RegExp object in JavaScript defines regular expressions. RegExp object methods and performs operation on expressions.

RegExp Methods


1) test()

The test() method searches for a pattern match in string. If match found returns true otherwise returns false.
var tx="hello";
var pat=/e/;
document.write("test() method returns:-", pat.test(tx));


Output::-
Regular expression test() method
Regular expression test() method


2) exec()

The exec() method searches for pattern match. If found then returns the matching string.

document.write("Found word in string is:-",/java/.exec("hello javascript"));

Output::-
Regular expression exec() method
Regular expression exec() method 


3) compile()

Compiles a regular expressions.

var tx="hello";
var pat=/e/;
pat.compile(pat);


4) toString()

Returns string value of regular expressions.

var tx="hello";
var pat=/e/;
document.write("string value of regular expression::-",pat.toString(tx));

Output::-
Regular expression toString() method
Regular expression toString() method 

RegExp properties

1) constructor

Returns function that creates RegExp object's prototype.

Syntax
RegExpObject.constructor


var c=new RegExp("Hello Javascript");
var res=c.constructor;
document.write(res);

Output::-
RegExp constructor property
RegExp constructor property

2) global

Checks for modifier ‘g’, if it is set returns true otherwise returns false.

Syntax
RegExpObject.global


var txt="hello javascript";
var patt=/hello/g;
var gm=patt.global;
document.write("if g modifier set return true otherwise false::-",gm);

Output::-
RegExp global property
RegExp global property

3) ignoreCase

Returns true if ‘i’ modifier is set, otherwise returns false.

Syntax
RegExpObject.ignoreCase


var txt="hello javascript";
var patt=/hello/g;
var gm=patt.global;
document.write("check for i modifier:--",patt.ignoreCase);

Output::-
RegExp ignoreCase property
RegExp ignoreCase property

4) lastIndex

Specifies the index where next match is start.
‘g’ modifier must set for working this property.

Syntax
RegExpObject.lastIndex

var t="hello, welcome to javascript";
var patrn=/come/g;
var tst=patrn.test(t);
document.write("Lastindex of 'come' is::-",patrn.lastIndex);

Output::-
RegExp lastIndex property
RegExp lastIndex property

5) multiline

If multiline property is set returns true otherwise returns false.

Syntax
RegExpObject.multiline


var t="hello, welcome to javascript";
var patrn=/come/g;
var tst=patrn.test(t);
document.write("return true if m is set, false for not set::--",patrn.multiline);

Output::-
RegExp multiline property
RegExp multiline property

6) source

Returns text of RegExp pattern.

Syntax
RegExp Object.source

var t="hello, welcome to javascript";
var patrn=/come/g;
var tst=patrn.test(t);
document.write("The text of regular expression ::-",patrn.source);

Output::-
RegExp source property
RegExp source property 

Conclusion

Regular expressions are made of  various types of characters, modifiers, quantifiers, Metacharacters and brackets. RegExp object methods and properties check for pattern, returns value. There is perticular form of patterns you can create pattern with combination of various types of characters.



Post a Comment

0 Comments