Last active
December 13, 2018 16:19
-
-
Save bhuizi/6909981 to your computer and use it in GitHub Desktop.
JavaScript RegEx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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