Reputation: 3045
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);
Upvotes: 1
Views: 315
Reputation: 1557
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:
haystack
/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' ]
Upvotes: 2