The RegExp object provides an easy way to extract pieces of a string that
match parts of your patterns. This is accomplished by grouping
(placing parentheses around) the portions of the pattern
you wish to extract.
For example, suppose
you wished to extract first names and phone numbers from strings that
look like this,
FirstName MiddleName LastName NN-NNNNNNNNNN
For Pattern below
/([A-Z]{2,}) ([A-Z]{2,}) ([A-Z]{2,}) ([0-9]{2}-[0-9]{10})/
When it is applied to a string, the parentheses induce
subexpressions. When a match is successful, these parenthesized subexpressions
can be referred to individually by using static properties $1 to $9 of the
RegExp class object.
Code:
var customer = "SANGRAM SHIAVJI DESAI 91-9890868345";
//does not work as we need groups
//var pattern = /[A-Z]{2,} [A-Z]{2,} [0-9]{2}-[0-9]{10}/;
var pattern = /([A-Z]{2,}) ([A-Z]{2,}) ([A-Z]{2,}) ([0-9]{2}-[0-9]{10})/;
if(pattern.test(customer)){
console.log("Matching SubString:")
console.log("First Name:" + RegExp.$1);
console.log("MIDDLE NAME:"+ RegExp.$2)
console.log("LAST NAME:"+ RegExp.$3)
console.log("PHONE NO:"+ RegExp.$4)
}
Output:
Matching SubString:
First Name:SANGRAM
MIDDLE NAME:SHIAVJI
LAST NAME:DESAI
PHONE NO:91-9890868345
AS RegExp.$n is deprecated we need another way to access sub expression.
way one:
"exec" will execute the RegEx on the string. It will return an array.
To access $1 we access the 1 element of the array.
If it didn't match, it will return null instead of an array, if it returns null,
then the || will make it return blank array [] so we don't get errors.
The || is an OR so if the first side is a falsey value (the undefined of the exec)
it will return the other side.
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
]
way two:
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 firstName = customer.replace(pattern,'$1');
var middleName = customer.replace(pattern,'$2');
var lastName = customer.replace(pattern,'$3');
var cellNo = customer.replace(pattern,'$4');
Output:
console.log("First Name:",firstName)
console.log("Middle Name:" + middleName)
console.log("Last Name"+ lastName)
console.log("Cell No:" + cellNo)
No comments:
Post a Comment