-
Notifications
You must be signed in to change notification settings - Fork 0
Regex 패턴
MinJeong Hong edited this page Jan 9, 2026
·
1 revision
-
.: 모든 문자 (줄바꿈 제외) -
^: 문자열의 시작 -
$: 문자열의 끝 -
*: 0개 이상 반복 -
+: 1개 이상 반복 -
?: 0개 또는 1개 -
{n}: 정확히 n개 -
{n,m}: n개 이상 m개 이하 -
[]: 문자 클래스 (하나의 문자) -
|: OR 연산자 -
(): 그룹화
-
\d: 숫자 (0-9) =[0-9] -
\D: 숫자가 아닌 것 =[^0-9] -
\w: 단어 문자 (영문, 숫자, _) =[a-zA-Z0-9_] -
\W: 단어 문자가 아닌 것 -
\s: 공백 문자 (스페이스, 탭, 줄바꿈) -
\S: 공백이 아닌 문자
// 정수만 (양수)
/^\d+$/.test('123') // true
/^\d+$/.test('123a') // false
/^\d+$/.test('-123') // false
// 음수 포함
/^-?\d+$/.test('-123') // true
/^-?\d+$/.test('123') // true
// 소수점 포함
/^\d+\.\d+$/.test('123.45') // true
/^-?\d+(\.\d+)?$/.test('123') // true (소수점 선택)
/^-?\d+(\.\d+)?$/.test('123.45') // true// 24시간 형식
/^([01]\d|2[0-3]):([0-5]\d)$/.test('09:30') // true
/^([01]\d|2[0-3]):([0-5]\d)$/.test('23:59') // true
/^([01]\d|2[0-3]):([0-5]\d)$/.test('24:00') // false
// 12시간 형식
/^(0?[1-9]|1[0-2]):([0-5]\d)$/.test('09:30') // true
/^(0?[1-9]|1[0-2]):([0-5]\d)$/.test('12:00') // true// YYYY-MM-DD
/^\d{4}-\d{2}-\d{2}$/.test('2024-12-25') // true
// MM/DD
/^\d{2}\/\d{2}$/.test('12/25') // true
// DD일
/^\d{1,2}일$/.test('25일') // true// 숫자,숫자,숫자 형식
/^\d+(,\d+)*$/.test('1,2,3') // true
/^\d+(,\d+)*$/.test('1,2,3,45') // true
/^\d+(,\d+)*$/.test('1') // true (하나만)
/^\d+(,\d+)*$/.test('1,') // false
// 공백 허용
/^\d+(\s*,\s*\d+)*$/.test('1, 2, 3') // true// [상품-수량] 형식
/^\[.+-.+\]$/.test('[콜라-3]') // true
/^\[.+-.+\]$/.test('[사이다-5]') // true
// 여러 개: [상품-수량],[상품-수량]
/^\[.+-.+\](,\[.+-.+\])*$/.test('[콜라-3],[사이다-5]') // true// 정확히 n자
/^.{5}$/.test('hello') // true (정확히 5자)
/^.{5}$/.test('hell') // false
// n자 이상 m자 이하
/^.{3,10}$/.test('hello') // true (3~10자)
/^.{3,10}$/.test('hi') // false (2자)// 영문만
/^[a-zA-Z]+$/.test('hello') // true
/^[a-zA-Z]+$/.test('hello1') // false
// 한글만
/^[가-힣]+$/.test('안녕') // true
/^[가-힣]+$/.test('안녕123') // false
// 영문 + 숫자
/^[a-zA-Z0-9]+$/.test('hello123') // true
/^[a-zA-Z0-9]+$/.test('hello-123') // false// 앞뒤 공백 없음
/^\S.*\S$|^\S$/.test('hello') // true
/^\S.*\S$|^\S$/.test(' hello') // false
// 공백 포함 안 함
/^\S+$/.test('hello') // true
/^\S+$/.test('hello world') // false
// 공백 허용
/^.+$/.test('hello world') // true// 대괄호 제거
'[콜라-3]'.replace(/[\[\]]/g, ''); // '콜라-3'
// 특수 문자 제거 (영문, 숫자, 한글만)
'hello-123!'.replace(/[^a-zA-Z0-9가-힣]/g, ''); // 'hello123'
// 숫자만 남기기
'abc123def'.replace(/\D/g, ''); // '123'const pattern = /^\d+$/;
pattern.test('123'); // true
pattern.test('abc'); // false'123abc456'.match(/\d+/g); // ['123', '456']
'hello'.match(/^[a-z]+$/); // ['hello'] 또는 null'hello-world'.replace(/-/g, '_'); // 'hello_world'
'[123]'.replace(/[\[\]]/g, ''); // '123'
'abc123'.replace(/\D/g, ''); // '123''1,2,3'.split(','); // ['1', '2', '3']
'1, 2, 3'.split(/\s*,\s*/); // ['1', '2', '3'] (공백 제거)function validateNumber(input) {
if (!/^\d+$/.test(input)) {
throw new Error('[ERROR] 숫자만 입력해주세요.');
}
}function validateTime(input) {
if (!/^([01]\d|2[0-3]):([0-5]\d)$/.test(input)) {
throw new Error('[ERROR] HH:MM 형식이 아닙니다.');
}
}function validateCommaNumbers(input) {
if (!/^\d+(,\d+)*$/.test(input)) {
throw new Error('[ERROR] 숫자,숫자 형식이 아닙니다.');
}
}function validateAmount(input) {
if (!/^\d+$/.test(input)) {
throw new Error('[ERROR] 숫자만 입력해주세요.');
}
const amount = Number(input);
if (amount % 1000 !== 0) {
throw new Error('[ERROR] 1000원 단위로 입력해주세요.');
}
}function validateDate(input) {
if (!/^\d{1,2}$/.test(input)) {
throw new Error('[ERROR] 숫자만 입력해주세요.');
}
const date = Number(input);
if (date < 1 || date > 31) {
throw new Error('[ERROR] 1~31 사이의 숫자여야 합니다.');
}
}function validateYesOrNo(input) {
const upper = input.toUpperCase().trim();
if (upper !== 'Y' && upper !== 'N') {
throw new Error('[ERROR] Y 또는 N만 입력해주세요.');
}
}function validatePurchaseForm(input) {
if (!/^\[.+-.+\](,\[.+-.+\])*$/.test(input)) {
throw new Error('[ERROR] [상품-수량] 형식이 아닙니다.');
}
}function validateNoSpace(input) {
if (!/^\S+$/.test(input)) {
throw new Error('[ERROR] 공백을 포함할 수 없습니다.');
}
}function validateLength(input, min, max) {
const pattern = new RegExp(`^.{${min},${max}}$`);
if (!pattern.test(input)) {
throw new Error(`[ERROR] ${min}~${max}자 사이여야 합니다.`);
}
}| 패턴 | 의미 | 예시 |
|---|---|---|
^\d+$ |
숫자만 |
123 ✅ |
^-?\d+$ |
숫자 (음수 포함) |
-123 ✅ |
^[a-zA-Z]+$ |
영문만 |
hello ✅ |
^[가-힣]+$ |
한글만 |
안녕 ✅ |
^\d{4}-\d{2}-\d{2}$ |
날짜 형식 |
2024-12-25 ✅ |
^([01]\d|2[0-3]):([0-5]\d)$ |
시간 형식 (24h) |
09:30 ✅ |
^\d+(,\d+)*$ |
쉼표 구분 숫자 |
1,2,3 ✅ |
^.{3,10}$ |
3~10자 |
hello ✅ |
^\S+$ |
공백 없음 |
hello ✅ |
^\[.+-.+\]$ |
[상품-수량] |
[콜라-3] ✅ |
// test() 결과를 직접 사용하지 않음
if (/^\d+$/.test(input)) {
} // ✅ 올바름
// 정규식에 변수 넣을 때
const num = '123';
/^num$/.test('123'); // false (문자열 'num'을 찾음)// 정규식 리터럴
/^\d+$/.test(input);
// RegExp 객체 (변수 사용 시)
const num = '123';
new RegExp(`^${num}$`).test('123') / // true
// 플래그 사용
hello /
gi.test(input); // g: 전역, i: 대소문자 무시✅ 숫자 검증: /^\d+$/
✅ 시간 형식: /^([01]\d|2[0-3]):([0-5]\d)$/
✅ 쉼표 구분: /^\d+(,\d+)*$/
✅ 특수문자 제거: .replace(/[\[\]]/g, '')
✅ 공백 제거: .replace(/\s/g, '') 또는 .trim()
✅ 길이 제한: /^.{n,m}$/