简体   繁体   English

使用 javascript 中的多个正则表达式模式匹配字符串

[英]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我在 indicesArr 中只得到 1 个indicesArr

[
  {
    "start":0,
    "end": 1
  }
]

Sandbox link沙盒链接

I want all the az characters should match and the email should match as well.我希望所有az字符都应该匹配,并且email也应该匹配。 I tried multiple approach, could not find it.我尝试了多种方法,找不到它。 Here in patterns array, pattern can be any regex, i just took two example.patterns数组中,模式可以是任何正则表达式,我只举了两个例子。

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;在循环中使用这一行if(.match.index ||;regexObj.lastIndex) break; will stop the loop when either of the statements in the if clause are true.当 if 子句中的任何一个语句为真时,将停止循环。

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.如果match.indexregexObj.lastIndex中的任何一个为 0,这将是 true 并且循环将停止,例如,如果第一个字符匹配,因为索引将为 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.因为 email 的第一个字符也将由[az]匹配,因此 email 将不匹配。

Note to omit the anchors ^ and $ from the email or else the email will only match if it is the only string.请注意省略 email 中的锚^$ ,否则 email 将仅在它是唯一字符串时匹配。

 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.我尝试使用 for 循环来获取与任何顺序相关的所有匹配项。

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沙盒

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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