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/flags | Description |
---|---|
i | Matches characters with case-insensitivity. |
g | Matches globally (not stop after first match). |
s | Matches characters starts with new line. |
m | Matches multiline characters. |
Quantifiers
Quantifiers | Description |
---|---|
+ | 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 | Description |
---|---|
\d | Matches only digits. |
\s | Matches white space characters (like tab, new line, and space) |
\b | Matches characters at beginning or at end of the string. |
\D | Matches non-digit characters. |
\S | Matches non-white space characters. |
Brackets
Brackets | Description |
---|---|
[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
RegExp Methods
1) test()
var tx="hello"; var pat=/e/; document.write("test() method returns:-", pat.test(tx));
Output::-
Regular expression test() method |
2) exec()
document.write("Found word in string is:-",/java/.exec("hello javascript"));
Output::-
Regular expression exec() method |
3) compile()
var tx="hello"; var pat=/e/; pat.compile(pat);
4) toString()
var tx="hello"; var pat=/e/; document.write("string value of regular expression::-",pat.toString(tx));
Output::-
Regular expression toString() method |
RegExp properties
1) constructor
var c=new RegExp("Hello Javascript"); var res=c.constructor; document.write(res);
Output::-
RegExp constructor property |
2) 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 |
3) 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 |
4) 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 |
5) 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 |
6) 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 |
0 Comments