Javascript RegExp - extract phone numbers from string

≯℡__Kan透↙ 提交于 2019-11-30 14:51:39

I 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\\-.].

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!