javascript - 从字符串中提取所有电话号码
问题描述
我有一个用于识别电话号码的正则表达式。
^(\+\d{1,2}\s)?\(?\d{3}\)?[\s.-]\d{3}[\s.-]\d{4}$
我想要做的是从字符串中提取所有电话号码并将其存储在一个数组中。我就是这样做的:
var rtel = new RegExp(/^(\+\d{1,2}\s)?\(?\d{3}\)?[\s.-]\d{3}[\s.-]\d{4}$/gi)
var r = "Inthe (555)-555-5555 pavithrarox @gmail.com strings below, you 'll find that the content of each 1. abc . pavithraprbd@gmail.com line is indented by some whitespace from the index of the line (the number is a part of the text to match). Try writing a pattern that can match each line regardless of how much whitespace is between the number and the content. Notice that the whitespace characters are just like any other character and the special metacharacters like the star and the plus can be used as well.".match(rtel);
但这仅在完整字符串仅与正则表达式匹配时才匹配。如何从字符串中获取所有电话号码。我错过了什么
解决方案
删除正则表达式中的^
(start) 和$
(end) 锚点。如果你把它们放进去,你的整个字符串必须匹配。
var anchors = new RegExp(/^(\+\d{1,2}\s)?\(?\d{3}\)?[\s.-]\d{3}[\s.-]\d{4}$/gi);
var no_anchors = new RegExp(/(\+\d{1,2}\s)?\(?\d{3}\)?[\s.-]\d{3}[\s.-]\d{4}/gi);
var testString1 = "Inthe (555)-555-5555 pavithrarox @gmail.com strings below, you 'll find that the content of each 1. abc . pavithraprbd@gmail.com line is indented by some whitespace from the index of the line (the number is a part of the text to match). Try writing a pattern that can match each line regardless of how much whitespace is between the number and the content. Notice that the whitespace characters are just like any other character and the special metacharacters like the star and the plus can be used as well.";
var testString2 = "(555)-555-5555";
console.log("testString1 - anchors: ", testString1.match(anchors)) // null
console.log("testString1 - no_anchors: ", testString1.match(no_anchors)) // ['(555)-555-5555']
console.log("testString2 - anchors: ", testString2.match(anchors)) // ['(555)-555-5555']
console.log("testString2 - no_anchors: ", testString2.match(no_anchors)) // ['(555)-555-5555']
推荐阅读
- botframework - 如何在 Microsoft Azure Bot QnA 框架中自定义“Hello and welcome”默认响应消息
- abi - cffi 外联 API 和 ABI 模式有什么区别?
- amazon-s3 - 使用 boto3 从 S3 中删除 Cache-Control 元数据
- c# - 当我不知道 Java 类在哪个包中时,如何找到它的包名?
- excel-formula - EXCEL isThisWeek()
- vb6 - VB6 - 更新查询匹配的多行
- reactjs - 处理连接到 Azure Cosmos DB (SQL API) 的 React 应用程序中的“访问被 CORS 策略阻止的获取”异常
- sql - 滚动值的复最小值
- sql - SQLite 使用 LIMIT 1 获得多个结果
- asp.net-mvc - [Authorize] 属性如何知道用户是否具有角色?