정규식 (regular expression)
The RegExp object is used for matching text with a pattern.
There are two ways to create a RegExp object:
a literal notation and a constructor.
The literal notation takes a pattern between two slashes, followed by optional flags, after the second slash.
The constructor function takes either a string or a RegExp object as its first parameter and a string of optional flags as its second parameter.
const re = /ab+c/i; // literal notation
// OR
const re = new RegExp("ab+c", "i"); // constructor with string pattern as first argument
// OR
const re = new RegExp(/ab+c/, "i"); // constructor with regular expression literal as first argument
영문 숫자 조합 8자리 이상
let reg = /^(?=.*[a-zA-Z])(?=.*[0-9]).{8,25}$/
영문 숫자 특수기호 조합 8자리 이상
let reg = /^(?=.*[a-zA-Z])(?=.*[!@#$%^*+=-])(?=.*[0-9]).{8,15}$/
해당 정규식은 다음과 같이 작동합니다.
^ : 문자열의 시작을 나타냅니다.
(?=.*\d) : 문자열에 숫자가 적어도 1개 이상 포함되어야 함을 나타냅니다.
(?=.*[a-zA-Z]) : 문자열에 영문자가 적어도 1개 이상 포함되어야 함을 나타냅니다.
(?=.*[\W_]) : 문자열에 특수문자가 적어도 1개 이상 포함되어야 함을 나타냅니다.
[a-zA-Z0-9\W_]{8,15} : 영문, 숫자, 특수문자 조합으로 이루어진 8~15자의 문자열을 나타냅니다.
$ : 문자열의 끝을 나타냅니다.
[!@#$%^*+=-] 이 부분이 특수문자까지 허용됩니다.
따라서, 해당 정규식은 영문, 숫자, 특수문자 조합으로 이루어진 8~15자의 문자열에 대해 검증을 수행합니다.
이메일
let reg = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$/i
reg.test() method
reg.test(string) => string이 정규식 reg를 만족하면 true, 아니면 false