HarmonyOS matchAll使用示例?

如题:HarmonyOS matchAll使用示例?

阅读 563
1 个回答
test2(){
  let text: string =
    "Please contact us at support@geeksforgeeks.org or at courses@geeksforgeeks.org";
  let emailRegex = /\b[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}\b/g;
  let emails = text.matchAll(emailRegex);
  for (const match of emails) {
    console.log("Found email address:", match[0]);
  }
}

test3(){
  let descriptions: string[] = [
    "Product 123 (ABC-DEF) is the best!",
    "Buy Product ID 456 now!",
    "This is not a product description.",
  ];
  const idRegex = /\bProduct\s+(\d+)\b|\bID\s+(\w+)\b/g;
  for (const description of descriptions) {
    const matches = description.matchAll(idRegex);
    for (const match of matches) {
      console.log("Product ID:", match[1] || match[2]);
    }
  }
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进