Last active
October 3, 2022 03:15
-
-
Save javierd79/06080b27fffb6de9da61cdedbde91e88 to your computer and use it in GitHub Desktop.
REGEXes to use in JS vanilla and React Project
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
/* REGEXes */ | |
const REGEX_INT = /^-?\d+$/; | |
const REGEX_EMAIL = /[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/g; | |
const REGEX_PASSWORD = /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[a-zA-Z]).{8,}$/gm; | |
const REGEX_IPv4 = /\b(?:(?:2(?:[0-4][0-9]|5[0-5])|[0-1]?[0-9]?[0-9])\.){3}(?:(?:2([0-4][0-9]|5[0-5])|[0-1]?[0-9]?[0-9]))\b/ig; | |
const REGEX_IPv6 = /(([a-fA-F0-9]{1,4}|):){1,7}([a-fA-F0-9]{1,4}|:)/gm; | |
const REGEX_HEX = /#?([\da-fA-F]{2})([\da-fA-F]{2})([\da-fA-F]{2})/g; | |
const REGEX_SCRIPT_TAG = /<.*?script.*\/?>/ig; | |
const REGEX_URL = /[-a-zA-Z0-9@:%_\+.~#?&//=]{2,256}\.[a-z]{2,4}\b(\/[-a-zA-Z0-9@:%_\+.~#?&//=]*)?/gi; | |
const REGEX_JS_COMMENT = /\/\*[\s\S]*?\*\/|\/\/.*/g; | |
const REGEX_PHONE_NUMBER = /^\s*(?:\+?(\d{1,3}))?([-. (]*(\d{3})[-. )]*)?((\d{3})[-. ]*(\d{2,4})(?:[-.x ]*(\d+))?)\s*$/gm; | |
const REGEX_CRYPTO_WALLET_ADDRESS = /([13][a-km-zA-HJ-NP-Z0-9]{26,33})/g; | |
const REGEX_RGB = /rgb\((?:([0-9]{1,2}|1[0-9]{1,2}|2[0-4][0-9]|25[0-5]), ?)(?:([0-9]{1,2}|1[0-9]{1,2}|2[0-4][0-9]|25[0-5]), ?)(?:([0-9]{1,2}|1[0-9]{1,2}|2[0-4][0-9]|25[0-5]))\)/g; | |
/* XXXXXXXXXXX */ | |
/* Evaluate */ | |
const isNumber = value => { | |
return REGEX_INT.test(value); | |
}; | |
const isEmail = email => { | |
return REGEX_EMAIL.test(email); | |
}; | |
const isPassword = password => { | |
return REGEX_PASSWORD.test(password); | |
}; | |
const isIPv4 = IPv4 => { | |
return REGEX_IPv4.test(IPv4); | |
}; | |
const isIPv6 = IPv6 => { | |
return REGEX_IPv6.test(IPv6); | |
}; | |
const isHex = hex => { | |
return REGEX_HEX.test(hex); | |
}; | |
const isScriptTag = scriptTag => { | |
return REGEX_SCRIPT_TAG.test(scriptTag); | |
}; | |
const isURL = URL => { | |
return REGEX_URL.test(URL); | |
}; | |
const isJsComment = JsComment => { | |
return REGEX_JS_COMMENT.test(JsComment); | |
}; | |
const isPhoneNumber = phoneNumber => { | |
return REGEX_PHONE_NUMBER.test(phoneNumber); | |
}; | |
const isCryptoWalletAddress = cryptoWalletAddress => { | |
return REGEX_BITCOIN_ADDRESS.test(cryptoWalletAddress); | |
}; | |
const isRGB = RGB => { | |
return REGEX_RGB.test(RGB); | |
} | |
/* XXXXXXXXXXX */ | |
/* Testing */ | |
isNumber('20'); // true | |
isEmail("javierd79@github.com"); // true | |
isPassword('Password123'); // true | |
isIPv4('192.168.0.1:3000'); // true | |
isIPv6('2001:0db8:0000:0000:0000:ff00:0042:8329'); // true | |
isHex('B00B69'); // true | |
isScriptTag('<script/>'); // true | |
isURL('https://github.com/javierd79'); // true | |
isJsComment('/* https://github.com/javierd79 */'); // true | |
isPhoneNumber('+58 412-000000'); // true | |
isCryptoWalletAddress('0x109051674D28A108d261a83eDf8e766D70603d3c'); // true | |
isRGB('rgb(255, 255, 255)'); // true | |
/* XXXXXXXXXXXX */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment