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:
- Remove any non-numerical characters from
haystack
- 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' ]