Last active
May 2, 2022 02:16
-
-
Save nitrotap/79d16b4cdb34bb0a10770e153b7551f9 to your computer and use it in GitHub Desktop.
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
/* | |
Matching a Hex Value – /^#?([a-f0-9]{6}|[a-f0-9]{3})$/ | |
Matching an Email – /^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/ | |
Matching a URL – /^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/ | |
Matching an HTML Tag – /^<([a-z]+)([^<]+)*(?:>(.*)<\/\1>|\s+\/>)$/ | |
*/ | |
const chkHex = (str) => { | |
const regexHex = new RegExp(/^#?([a-f0-9]{6}|[a-f0-9]{3})$/); | |
return regexHex.test(str); | |
}; | |
const chkEmail = (str) => { | |
const regexEmail = new RegExp(/^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/g, 'i'); | |
return regexEmail.test(str); | |
}; | |
const chkUrl = (str) => { | |
const regexUrl = new RegExp(/^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/, 'g'); | |
return regexUrl.test(str); | |
}; | |
const chkHTMLTag = (str) => { | |
const regexHTMLTag = new RegExp(/^<([a-z]+)([^<]+)*(?:>(.*)<\/\1>|\s+\/>)$/, 'ig'); | |
return regexHTMLTag.test(str); | |
}; | |
module.exports = { chkHex, chkEmail, chkUrl, chkHTMLTag }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment