1

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);
JPI93
  • 1,507
  • 5
  • 10
Soth
  • 2,901
  • 2
  • 27
  • 27
  • This will help, read: https://stackoverflow.com/questions/123559/how-to-validate-phone-numbers-using-regex/63771966#63771966 – DigitShifter Oct 09 '20 at 05:26
  • Not sure why it was downvoted.. it was to search a field in a database for a particular number but it only accepts regex. I'm not using it to see if it is a valid number. – Soth Oct 09 '20 at 06:04
  • Are you trying to match a string to a particular number? Or you have some more general rules? – Alexey R. Oct 09 '20 at 11:00
  • Yes it is trying to find a particular number. – Soth Oct 12 '20 at 02:27

1 Answers1

2

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.

e.g

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' ]

JPI93
  • 1,507
  • 5
  • 10