Skip to content

Instantly share code, notes, and snippets.

@bhuizi
Last active December 13, 2018 16:19
Show Gist options
  • Save bhuizi/6909981 to your computer and use it in GitHub Desktop.
Save bhuizi/6909981 to your computer and use it in GitHub Desktop.
JavaScript RegEx
//file ext
(/\.(gif|jpg|jpeg|tiff|png)$/i).test(filename)
//match numbers
var regex = new RegExp("\\+?\\(?\\d*\\)? ?\\(?\\d+\\)?\\d*([\\s./-]?\\d{2,})+", "g")
,string = "testing testing one 800-999-9999"
,arr = string.match(regex);
//remove numbers from string => format
var string = "diplomat 999-555-5555";
str = string.replace(/[^\d.]/g, "");
console.log(str);
var regexObj = /^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/;
var formattedNumber = "";
if (regexObj.test(str)) {
formattedNumber =
str.replace(regexObj, "($1) $2-$3");
} else {
// Invalid phone number
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment