简体   繁体   中英

Regex to match a phone number ignoring non digits

Im trying to match a phone number in regex when there is a non standard format in the data. How would you write a regex to match 0408111 in all 4 of these examples?

(04) 0811 111
0408-111111
0408111111
0a4b0c8d1e1f1g

The best I came up with is:

/0[^\d]*4[^\d]*0[^\d]*8[^\d]*1[^\d]*1[^\d]*1/g

But is there a better way?

 var haystack = ` (04) 0811 111 0408-111111 0408111111 0a4b0c8d1e1f1g `; var needle = /0[^\d]*4[^\d]*0[^\d]*8[^\d]*1[^\d]*1[^\d]*1/g; var result = haystack.match(needle); console.log(result);

Given the example string from your question.

let haystack = `
(04) 0811 111
0408-111111
0408111111
0a4b0c8d1e1f1g
`

One solution to return the string '0408111' each time digits appear in this order within each line of haystack whilst discounting any non-numerical interspersed within each line would be to:

  1. Remove any non-numerical characters from haystack
  2. Return all matches of the pattern /0408111/g
let result = haystack.replace(/[^\d\n]/g, '').match(/0408111/g)

Given haystack as above, result will be [ '0408111', '0408111', '0408111', '0408111' ] .

Since you say that you are using this to search for phone numbers within each line of the input string and the example you gave in your question is seeking a match on 7 consecutive digits in each line regardless of any non-numeric characters. The above code could be adjusted as to match the first 7 digits in each line once non-numeric characters have been removed - by matching on /\d{7}/g instead /0408111/g pattern.

eg

let haystack = `
(04) 0811 111
0454-14717181
0768551351111
0tY4lopj9pjo5567
0a4b0c8d1e1f1g
123456
`

let result = haystack.replace(/[^\d\n]/g, '').match(/\d{7}/g)

Here result will be [ '0408111', '0454147', '0768551', '0495567', '0408111' ]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM