Javascript RegExp - extract phone numbers from string

后端 未结 1 463
攒了一身酷
攒了一身酷 2021-01-03 03:25

I have the following working regex to extract a lot of phone numbers in different formats.

See here: http://jsfiddle.net/SB5Ly/4/

var regex = new Reg         


        
相关标签:
1条回答
  • 2021-01-03 03:50

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

    0 讨论(0)
提交回复
热议问题