const regex = /\+?[\d]?\s*\(?\d{3}\)?[\s\.-]?\d{3}[\s\.-]?\d{4}/gm;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('\\+?[\\d]?\\s*\\(?\\d{3}\\)?[\\s\\.-]?\\d{3}[\\s\\.-]?\\d{4}', 'gm')
const str = `Task: Return only the residents of Spencerport, NY who have phone numbers listed. Include the name, address and phone number of the individual(s).
----------------------------------------------------------------
Public Records and Phone Records Listings
DEFRANK, EDMUND
823 WASHINGTON ST, SPENCERPORT, NY 14559-9703.
Phone Number 585-352-9218
ODELL, E
BRIGHTON, NY 12970.
Phone Number 585-327-3686
DEFRANCO, FRANK
6 REX LN, SPENCERPORT, NY 14559-9700
DR. OBRECT, MICHAEL
200 RED CREEK DR, HENRIETTA, NY 14467
Phone Number +1 (585) 334-0352
FRANCIS, PAUL
37 TROTTER DR, HENRIETTA, NY 14467-9769.
Phone Number 585-334-7733
CARLSON, BRITA
1 BURKE LN #D, SPENCERPORT, NY 14559-1546.
Phone Number +1 (585) 3529383
EDWARDS, SUSAN
4621 W RIDGE RD, SPENCERPORT, NY 14559-1552.
Phone Number
CARLSON, JEFF
21 NEVINS RD, HENRIETTA, NY 14467-9307.
Phone Number 585-359-9545
FRANCIS, RICHARD
13 LINDA LN, SPENCERPORT, NY 14559-1613.
Phone Number
DEFREZE, GREGORY
10 BIG RIDGE RD, SPENCERPORT, NY 14559-1219.
Phone Number 5853520978
ENGLEHARDT, HERBERT
75 TOTTENHAM RD, BRIGHTON, NY 14610-2244.
Phone Number 585-482-2955
CARTER, GARY
204 PINE HILL RD, SPENCERPORT, NY 14559-1010.
Phone Number (585)352-8826
DEGEN, MARK
27 COBRA DR, HENRIETTA, NY 14467-9512.
Phone Number 585-321-3524
DEGENNARO, VICTOR
537 WASHINGTON ST, SPENCERPORT, NY 14559-9539.
Phone Number +1 (585) 352-3824
FRANCIS, THOMAS
4812 LYELL RD, SPENCERPORT, NY 14559-2014.
Phone Number 585-352-4758
CARTER, LINDA
50 JORDACHE LN, SPENCERPORT, NY 14559-2059.
Phone Number 585-352-4913`;
// Reset `lastIndex` if this regex is defined globally
// regex.lastIndex = 0;
let m;
while ((m = regex.exec(str)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
// The result can be accessed through the `m`-variable.
m.forEach((match, groupIndex) => {
console.log(`Found match, group ${groupIndex}: ${match}`);
});
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for JavaScript, please visit: https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions