admin管理员组文章数量:1414922
I have the following working regex to extract a lot of phone numbers in different formats.
See here: /
var regex = new RegExp(
"\\+?\\(?\\d*\\)? ?\\(?\\d+\\)?\\d*([\\s./-]?\\d{2,})+",
"g"
);
....
(If the script didn't load, press the "Run" button from the top menu)
As you can see in the example (by following the link), the last 2-3 phone number formats (var phoneNumbers) doesn't match the used regex. You can test the regex modifying it in the script and running it.
So i need a regex that match all the enumerated phone number formats (to extract them from an entire webpage (document.body.innerHTML)).
I have the following working regex to extract a lot of phone numbers in different formats.
See here: http://jsfiddle/SB5Ly/4/
var regex = new RegExp(
"\\+?\\(?\\d*\\)? ?\\(?\\d+\\)?\\d*([\\s./-]?\\d{2,})+",
"g"
);
....
(If the script didn't load, press the "Run" button from the top menu)
As you can see in the example (by following the link), the last 2-3 phone number formats (var phoneNumbers) doesn't match the used regex. You can test the regex modifying it in the script and running it.
So i need a regex that match all the enumerated phone number formats (to extract them from an entire webpage (document.body.innerHTML)).
Share Improve this question edited Sep 27, 2013 at 6:57 Lorand Tamas asked Sep 25, 2013 at 13:22 Lorand TamasLorand Tamas 511 silver badge5 bronze badges1 Answer
Reset to default 3I don't know how prescriptive you want to be, but this matches all your examples:
var regex = new RegExp("\\+?\\(?\\d*\\)? ?\\(?\\d+\\)?\\d*([\\s./-]?\\d{2,})+", "g");
See a live demo of this regex.
One small bug with your regex: When wanting to include a literal dash in a character class, either escape it or place it first or last.
You had [\\s-.]
, which is incorrect. It should be either [\\s.-]
, [-\\s.]
or [\\s\\-.]
.
本文标签: regexJavascript RegExpextract phone numbers from stringStack Overflow
版权声明:本文标题:regex - Javascript RegExp - extract phone numbers from string - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745183278a2646564.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论