CodesJava

Easy learning with example program codes

JavaScript RegExp tutorial

 

A regular expression refers to the object which represents the pattern of characters.

JavaScript provides the RegEx class which provides the pattern-matching and search-and-replace functionality on text.

Syntax

/pattern/modifiers;

 

Modifiers

Modifiers are used to perform case-insensitive and global searches.

ModifierDescription
gIt is used to find all matches.
iIt is used to perform case-insensitive matching
mIt is used to perform multi line matching.

Brackets

Brackets are used to find the range of characters.

ExpressionDescription
[abc]It is used to find any character between the brackets.
[^abc]It is used to find any character not between the brackets.
[0-9]It is used to find any character between the brackets (any digit).
[^0-9]It is used to find any character not between the brackets (any non-digit)
(x|y)It is used to find any of the alternatives specified.

 

Metacharacters

Metacharacters are the alphabetical characters preceded by a backslash with a special meaning.

MetacharacterDescription
.It is used to find a single character. It exclude newline or line terminator
\wIt is used to find a word character.
\WIt is used to find a non-word character.
\dIt is used to find a digit.
\DIt is used to find a non-digit character.
\sIt is used to find a white space character.
\SIt is used to find a non-white space character.
\bIt is used to find a match at the beginning/end of a word.
\BIt is used to find a match not at the beginning/end of a word.
\0It is used to find a NULL character.
\nIt is used to find a new line character.
\fIt is used to find a form feed character.
\rIt is used to find a carriage return character.
\tIt is used to find a tab character.
\vIt is used to find a vertical tab character.
\xxxIt is used to find the character specified by an octal number xxx.
\xddIt is used to find the character specified by a hexadecimal number dd.
\uddddIt is used to find  the Unicode character specified by a hexadecimal number dddd.

Quantifiers

QuantifierDescription
n+It matches any string that contains at least one n.
n*It matches any string that contains zero or more occurrences of n.
n?It matches any string that contains zero or one occurrences of n.
n{X}It matches any string that contains a sequence of X n‘s.
n{X,Y}It matches any string that contains a sequence of X to Y n‘s.
n{X,}It matches any string that contains a sequence of at least X n‘s.
n$It matches any string with n at the end of it.
^nIt matches any string with n at the beginning of it.
?=nIt matches any string that is followed by a specific string n.
?!nIt matches any string that is not followed by a specific string n.

RegExp Object Properties

PropertyDescription
constructorIt returns the function that created the RegExp object’s prototype.
globalIt used to check whether the “g” modifier is set.
ignoreCaseIt used to check whether the “i” modifier is set.
lastIndexIt is used to get the index at which to start the next match.
multilineIt used to check whether the “m” modifier is set.
sourceIt is used to get the text of the RegExp pattern.

RegExp Object Methods

RegExp.exec() Method:
Use: To get an array containing all the matched groups for a specified string.

RegExp.test() Method:
Use: To search a match between a regular expression and a specified string.

RegExp.toString() Method:
Use: To retrieve a string to represent the regular expression.

Example

<!DOCTYPE html>
<html>
<body>
<script>
var string  = "HELLO WORLD";  
var regExObj = new RegExp( "HELLO", "g" );  
var print = regExObj.exec(string);  
document.write("Returned value : " +  print);   
</script>
</body>
</html>

Output

Returned value : HELLO
Please follow and like us:
error








Copyright © 2019 CodesJava DMCA.com Protection Status