简体   繁体   中英

Match strings with multiple regex patterns in javascript

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 abinas@gmail.com"
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
  }
]

Sandbox link

I want all the az 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.

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 abinas@gmail.com";
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 abinas@gmail.com"; let emailAddress = extractEmails( str ); let remainingString = str.replace( emailAddress, '' ); console.log( "Original string: " + str ); console.log( "Email address: " + emailAddress ); console.log( "Remaining string: " + remainingString );

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 [az] 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 abinas@gmail.com" let patterns = [ "[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[AZ]{2,4}", "[az]" ]; 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)

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 abinas@gmail.com"
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)

Sandbox

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM