I was trying to match multiple regex pattern on a string to find start and end index.
let str = "I am abinas patra and my email is [email protected]"
let patterns = [
"[a-z]",
"^[A-Z0-9._% -] @[A-Z0-9.-] \.[A-Z]{2,4}$"
];
let regexObj = new RegExp(patterns.join("|"), "gmi");
let match, indicesArr=[];
while ((match = regexObj.exec(str))) {
let obj = { start: match.index, end: regexObj.lastIndex }
indicesArr.push(obj);
if(!match.index || !regexObj.lastIndex) break;
}
I am getting only 1 object in the indicesArr
which is
[
{
"start":0,
"end": 1
}
]
I want all the a-z
characters should match and the email
should match as well. I tried multiple approach, could not find it.
Here in patterns
array, pattern can be any regex, i just took two example.
CodePudding user response:
Use this instead:
function extractEmails(text) {
return text.match(/([a-zA-Z0-9._-] @[a-zA-Z0-9._-] \.[a-zA-Z0-9_-] )/gi);
}
let str = "I am abinas patra and my email is [email protected]";
let emailAddress = extractEmails( str );
let remainingString = str.replace( emailAddress, '' );
function extractEmails(text) {
return text.match(/([a-zA-Z0-9._-] @[a-zA-Z0-9._-] \.[a-zA-Z0-9_-] )/gi);
}
let str = "I am abinas patra and my email is [email protected]";
let emailAddress = extractEmails( str );
let remainingString = str.replace( emailAddress, '' );
console.log( "Original string: " str );
console.log( "Email address: " emailAddress );
console.log( "Remaining string: " remainingString );
CodePudding user response:
Using this line in the loop if(!match.index || !regexObj.lastIndex) break;
will stop the loop when either of the statements in the if clause are true.
If either the match.index
or regexObj.lastIndex
is zero, this will be true and the loop will stop, and this will happen for example if there is a match for the first character as the index will be 0.
You can also switch the order of the patterns, putting the most specific one first. Because the first char of the email will also be matched by [a-z]
so the email will otherwise not be matched.
Note to omit the anchors ^
and $
from the email or else the email will only match if it is the only string.
let str = "I am abinas patra and my email is [email protected]"
let patterns = [
"[A-Z0-9._% -] @[A-Z0-9.-] \.[A-Z]{2,4}",
"[a-z]"
];
let regexObj = new RegExp(patterns.join("|"), "gmi");
let match, indicesArr = [];
while ((match = regexObj.exec(str))) {
let obj = {
start: match.index,
end: regexObj.lastIndex
}
indicesArr.push(obj);
}
console.log(indicesArr)
CodePudding user response:
I tried with for loop to get all the matches with respect to any order.
let str = "I am abinas patra and my email is [email protected]"
let patterns = [
"[a-z]",
"[A-Z0-9._% -] @[A-Z0-9.-] \.[A-Z]{2,4}"
];
let match, indicesArr = [];
for(let i=0;i<patterns.length; i ){
let regexObj = new RegExp(patterns[i], "gmi");
while ((match = regexObj.exec(str)) !== null) {
let obj = {
start: match.index,
end: regexObj.lastIndex
}
indicesArr.push(obj);
}
}
console.log(indicesArr)