Skip to content

Instantly share code, notes, and snippets.

@aal89
Last active November 2, 2018 09:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aal89/eba0891c64841ae4046b7b50dfb0e7be to your computer and use it in GitHub Desktop.
Save aal89/eba0891c64841ae4046b7b50dfb0e7be to your computer and use it in GitHub Desktop.
A Javascript regex to filter all dutch cellphone numbers from a particular text and filter for uniqueness. Works for no mathces, one match and multiple matches.
var text = `
the text containing a couple of 0612345678 numbers. like so: 0612345678 and so:
0687654321
and
another
example
0674839201
`;
// Approach one: returns an ES6 Set.
new Set(text.match(/06[0-9]{8}/gm)); //=> Set(3) {"0612345678", "0687654321", "0674839201"}
// Approach two: returns an array only containing unique values.
(text.match(/06[0-9]{8}/gm) || []).filter((e,i,a) => a.indexOf(e) == i); //=> ["0612345678", "0687654321", "0674839201"]
@aal89
Copy link
Author

aal89 commented Nov 2, 2018

Shell approach to getting unique cellphone numbers from any file (here a .log file), sorted and filtered for unique ones.

$ < [FILENAME].log grep -oP '06[0-9]{8}' | sort --unique

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment