Search This Blog

2024/03/21

Javascript Regular Expression: exec() method & global flag

The RegExp object also provides a method called exec(). This method is
used when you’d like to test whether a given string matches a pattern
and would additionally like more information about the match

two ways to use exec method

one way
var pattern = /http:.*/
pattern.exec("http://www.w3.org/")

other way
var pattern = /http:.*/
pattern("http://www.w3.org/")

The exec() method returns an array with a variety of properties. The [0]
position of the array will contain the last matched characters, and the [1],…[n]
positions will show any parenthesized substring matches similar to the $1…$9.

The input property will show the original input string, while the
index property will show the character index (starting from 0) at which the
matching portion of the string begins.

Code:
var customer = "SANGRAM SHIAVJI DESAI 91-9890868345";
var pattern = /([A-Z]{2,}) ([A-Z]{2,}) ([A-Z]{2,}) ([0-9]{2}-[0-9]{10})/;
var resultText = pattern.exec(customer) || [] ;

console.log(resultText)

Output:
[
'SANGRAM SHIAVJI DESAI 91-9890868345',
'SANGRAM',
'SHIAVJI',
'DESAI',
'91-9890868345',
index: 0,
input: 'SANGRAM SHIAVJI DESAI 91-9890868345',
groups: undefined
]


exec( ) and the Global Flag:

Sometimes you might wish to extract not just the first occurrence of a
pattern in a string, but each occurrence of it. Adding the global flag (g)
to a regularexpression indicates the intent to search for every occurrence
(that is, globally) instead of just the first.

Code;
var sentence ="The number with eight sum are 8 26 35 44"
var pattern = /\d+/

console.log("-----------------------------------------------")
console.log("Not Global Pattern")
console.log("First:" + pattern.exec(sentence))
console.log("Second:" + pattern.exec(sentence))
console.log( "Third:" + pattern.exec(sentence))

console.log("-----------------------------------------------")

var globalPattern = /\d+/g
console.log("Global Pattern")
//get occurences but we need tyo run till end
console.log("First:" + globalPattern.exec(sentence))
console.log("Second:" + globalPattern.exec(sentence))
console.log( "Third:" + globalPattern.exec(sentence))
console.log("-----------------------------------------------")

var newSentence ="The number with nine sum are 9 27 36 45"
var newGlobalpattern = /\d+/g

//get all occurences dynamically using loop
var counter =1
console.log("-----------------------------------------------")
while(x = newGlobalpattern.exec(newSentence)){
console.log("index No "+ counter + ":" + x[0])
}
console.log("-----------------------------------------------")

Output:
-----------------------------------------------
Not Global Pattern
First:8
Second:8
Third:8
-----------------------------------------------
Global Pattern
First:8
Second:26
Third:35
-----------------------------------------------
-----------------------------------------------
index No 1:9
index No 1:27
index No 1:36
index No 1:45
-----------------------------------------------

when the global flag is set, the exec() starts searching where the previous
match ended.Without the global flag, exec() always returns the first matching
portion of the string.


No comments:

Post a Comment