❗ 31.1 μ •κ·œ ν‘œν˜„μ‹μ΄λž€?

μ •κ·œ ν‘œν˜„μ‹(regular expression)은 μΌμ •ν•œ νŒ¨ν„΄μ„ 가진 λ¬Έμžμ—΄μ˜ 집함을 ν‘œν˜„ν•˜κΈ° μœ„ν•΄ μ‚¬μš©ν•˜λŠ” ν˜•μ‹ μ–Έμ–΄(formal language)λ‹€. μ •κ·œ ν‘œν˜„μ‹μ€ λ¬Έμžμ—΄μ„ λŒ€μƒμœΌλ‘œ νŒ¨ν„΄ 맀칭 κΈ°λŠ₯을 μ œκ³΅ν•œλ‹€. νŒ¨ν„΄ 맀칭 κΈ°λŠ₯μ΄λž€ νŠΉμ • νŒ¨ν„΄κ³Ό μΌμΉ˜ν•˜λŠ” λ¬Έμžμ—΄μ„ κ²€μƒ‰ν•˜κ±°λ‚˜ μΆ”μΆœ λ˜λŠ” μΉ˜ν™˜ν•  수 μžˆλŠ” κΈ°λŠ₯을 λ§ν•œλ‹€.

❗ 31.2 μ •κ·œ ν‘œν˜„μ‹μ˜ 생성

μ •κ·œ ν‘œν˜„μ‹ 객체(RegExp 객체)λ₯Ό μƒμ„±ν•˜κΈ° μœ„ν•΄μ„œλŠ” μ •κ·œ ν‘œν˜„μ‹ λ¦¬ν„°λŸ΄κ³Ό RegExp μƒμ„±μž ν•¨μˆ˜λ₯Ό μ‚¬μš©ν•  수 μžˆλ‹€. 일반적인 방법은 μ •κ·œ ν‘œν˜„μ‹ λ¦¬ν„°λŸ΄μ„ μ‚¬μš©ν•˜λŠ” 것이닀. μ •κ·œ ν‘œν˜„μ‹ λ¦¬ν„°λŸ΄μ€ λ‹€μŒκ³Ό 같이 ν‘œν˜„ν•œλ‹€.

const target = 'Is this all there is?';

// νŒ¨ν„΄: is
// ν”Œλž˜κ·Έ: i => λŒ€μ†Œλ¬Έμžλ₯Ό κ΅¬λ³„ν•˜μ§€ μ•Šκ³  κ²€μƒ‰ν•œλ‹€.
const regexp = /is/i;

// test λ©”μ„œλ“œλŠ” target λ¬Έμžμ—΄μ— λŒ€ν•΄ μ •κ·œν‘œν˜„μ‹ regexp의 νŒ¨ν„΄μ„ κ²€μƒ‰ν•˜μ—¬ 맀칭 κ²°κ³Όλ₯Ό λΆˆλ¦¬μ–Έ κ°’μœΌλ‘œ λ°˜ν™˜ν•œλ‹€.
regexp.test(target); // -> true

RegExp μƒμ„±μž ν•¨μˆ˜λ₯Ό μ‚¬μš©ν•˜μ—¬ RegExp 객체λ₯Ό 생성할 μˆ˜λ„ μžˆλ‹€.

const target = 'Is this all there is?';

const regexp = new RegExp(/is/i); // ES6
// const regexp = new RegExp(/is/, 'i');
// const regexp = new RegExp('is', 'i');

regexp.test(target); // -> true

RegExp μƒμ„±μž ν•¨μˆ˜λ₯Ό μ‚¬μš©ν•˜λ©΄ λ³€μˆ˜λ₯Ό μ‚¬μš©ν•΄ λ™μ μœΌλ‘œ RegExp 객체λ₯Ό 생성할 수 μžˆλ‹€.

const count = (str, char) => (str.match(new RegExp(char, 'gi')) ?? []).length;

count('Is this all there is?', 'is'); // -> 3
count('Is this all there is?', 'xx'); // -> 0

❗ 31.3 RegExp λ©”μ„œλ“œ

μ •κ·œ ν‘œν˜„μ‹μ„ μ‚¬μš©ν•˜λŠ” λ©”μ„œλ“œλŠ” RegExp.prototype.exec, RegExp.prototype.test, String.prototype.match, String.prototype.replace, String.prototype.search, String.prototype.split 등이 μžˆλ‹€.

βœ” 31.3.1 RegExp.prototype.exec

exec λ©”μ„œλ“œλŠ” 인수둜 전달받은 λ¬Έμžμ—΄μ— λŒ€ν•΄ μ •κ·œ ν‘œν˜„μ‹μ˜ νŒ¨ν„΄μ„ κ²€μƒ‰ν•˜μ—¬ 맀칭 κ²°κ³Όλ₯Ό λ°°μ—΄λ‘œ λ°˜ν™˜ν•œλ‹€. 맀칭 κ²°κ³Όκ°€ μ—†λŠ” 경우 null을 λ°˜ν™˜ν•œλ‹€.

const target = 'Is this all there is?';
const regExp = /is/;

regExp.exec(target); // -> ["is", index: 5, input: "Is this all there is?", groups: undefined]

βœ” 31.3.2 RegExp.prototype.test

test λ©”μ„œλ“œλŠ” 인수둜 전달받은 λ¬Έμžμ—΄μ— λŒ€ν•΄ μ •κ·œ ν‘œν˜„μ‹μ˜ νŒ¨ν„΄μ„ κ²€μƒ‰ν•˜μ—¬ 맀칭 κ²°κ³Όλ₯Ό λΆˆλ¦¬μ–Έ κ°’μœΌλ‘œ λ°˜ν™˜ν•œλ‹€.

const target = 'Is this all there is?';
const regExp = /is/;

regExp.test(target); // -> true

βœ” 31.3.3 String.prototype.match

String ν‘œμ€€ 빌트인 객체가 μ œκ³΅ν•˜λŠ” match λ©”μ„œλ“œλŠ” λŒ€μƒ λ¬Έμžμ—΄κ³Ό 인수둜 전달받은 μ •κ·œ ν‘œν˜„μ‹κ³Όμ˜ 맀칭 κ²°κ³Όλ₯Ό λ°°μ—΄λ‘œ λ°˜ν™˜ν•œλ‹€.

const target = 'Is this all there is?';
const regExp = /is/;

target.match(regExp); // -> ["is", index: 5, input: "Is this all there is?", groups: undefined]

exec λ©”μ„œλ“œλŠ” λ¬Έμžμ—΄ λ‚΄μ˜ λͺ¨λ“  νŒ¨ν„΄μ„ κ²€μƒ‰ν•˜λŠ” g ν”Œλž˜κ·Έλ₯Ό 지정해도 첫 번째 맀칭 결과만 λ°˜ν™˜ν•œλ‹€. ν•˜μ§€λ§Œ String.prototype.match λ©”μ„œλ“œλŠ” gν”Œλž˜κ·Έκ°€ μ§€μ •λ˜λ©΄ λͺ¨λ“  맀칭 κ²°κ³Όλ₯Ό λ°°μ—΄λ‘œ λ°˜ν™˜ν•œλ‹€.

const target = 'Is this all there is?';
const regExp = /is/g;

target.match(regExp); // -> ["is", "is"]

❗ 31.4 ν”Œλž˜κ·Έ

νŒ¨ν„΄κ³Ό ν•¨κ»˜ μ •κ·œ ν‘œν˜„μ‹μ„ κ΅¬μ„±ν•˜λŠ” ν”Œλž˜κ·ΈλŠ” μ •κ·œ ν‘œν˜„μ‹μ˜ 검색 방식을 μ„€μ •ν•˜κΈ° μœ„ν•΄ μ‚¬μš©ν•œλ‹€.

const target = 'Is this all there is?';

// target λ¬Έμžμ—΄μ—μ„œ is λ¬Έμžμ—΄μ„ λŒ€μ†Œλ¬Έμžλ₯Ό κ΅¬λ³„ν•˜μ—¬ ν•œ 번만 κ²€μƒ‰ν•œλ‹€.
target.match(/is/);
// -> ["is", index: 5, input: "Is this all there is?", groups: undefined]

// target λ¬Έμžμ—΄μ—μ„œ is λ¬Έμžμ—΄μ„ λŒ€μ†Œλ¬Έμžλ₯Ό κ΅¬λ³„ν•˜μ§€ μ•Šκ³  ν•œ 번만 κ²€μƒ‰ν•œλ‹€.
target.match(/is/i);
// -> ["Is", index: 0, input: "Is this all there is?", groups: undefined]

// target λ¬Έμžμ—΄μ—μ„œ is λ¬Έμžμ—΄μ„ λŒ€μ†Œλ¬Έμžλ₯Ό κ΅¬λ³„ν•˜μ—¬ μ „μ—­ κ²€μƒ‰ν•œλ‹€.
target.match(/is/g);
// -> ["is", "is"]

// target λ¬Έμžμ—΄μ—μ„œ is λ¬Έμžμ—΄μ„ λŒ€μ†Œλ¬Έμžλ₯Ό κ΅¬λ³„ν•˜μ§€ μ•Šκ³  μ „μ—­ κ²€μƒ‰ν•œλ‹€.
target.match(/is/ig);
// -> ["Is", "is", "is"]

❗ 31.5 νŒ¨ν„΄

μ •κ·œ ν‘œν˜„μ‹μ˜ νŒ¨ν„΄μ€ λ¬Έμžμ—΄μ˜ μΌμ •ν•œ κ·œμΉ™μ„ ν‘œν˜„ν•˜κΈ° μœ„ν•΄ μ‚¬μš©ν•˜λ©°,μ •κ·œ ν‘œν˜„μ‹μ˜ ν”Œλž˜κ·ΈλŠ” μ •κ·œ ν‘œν˜„μ‹μ˜ 검색 방식을 μ„€μ •ν•˜κΈ° μœ„ν•΄ μ‚¬μš©ν•œλ‹€.

νŒ¨ν„΄μ€ /둜 μ—΄κ³  λ‹«μœΌλ©° λ¬Έμžμ—΄μ˜ λ”°μ˜΄ν‘œλŠ” μƒλž΅ν•œλ‹€. λ˜ν•œ νŒ¨ν„΄μ€ νŠΉλ³„ν•œ 의미λ₯Ό κ°€μ§€λŠ” λ©”νƒ€λ¬Έμž λ˜λŠ” 기호둜 ν‘œν˜„ν•  수 μžˆλ‹€.

βœ” 31.5.1 λ¬Έμžμ—΄ 검색

μ •κ·œ ν‘œν˜„μ‹μ˜ νŒ¨ν„΄μ— 문자 λ˜λŠ” λ¬Έμžμ—΄μ„ μ§€μ •ν•˜λ©΄ 검색 λŒ€μƒ λ¬Έμžμ—΄μ—μ„œ νŒ¨ν„΄μœΌλ‘œ μ§€μ •ν•œ 문자 λ˜λŠ” λ¬Έμžμ—΄μ„ κ²€μƒ‰ν•œλ‹€.

const target = 'Is this all there is?';

// 'is' λ¬Έμžμ—΄κ³Ό λ§€μΉ˜ν•˜λŠ” νŒ¨ν„΄. ν”Œλž˜κ·Έκ°€ μƒλž΅λ˜μ—ˆμœΌλ―€λ‘œ λŒ€μ†Œλ¬Έμžλ₯Ό κ΅¬λ³„ν•œλ‹€.
const regExp = /is/;

// targetκ³Ό μ •κ·œ ν‘œν˜„μ‹μ΄ λ§€μΉ˜ν•˜λŠ”μ§€ ν…ŒμŠ€νŠΈν•œλ‹€.
regExp.test(target); // -> true

// targetκ³Ό μ •κ·œ ν‘œν˜„μ‹μ˜ 맀칭 κ²°κ³Όλ₯Ό κ΅¬ν•œλ‹€.
target.match(regExp);
// -> ["is", index: 5, input: "Is this all there is?", groups: undefined]

λŒ€μ†Œλ¬Έμžλ₯Ό κ΅¬λ³„ν•˜μ§€ μ•Šκ³  κ²€μƒ‰ν•˜λ €λ©΄ ν”Œλž˜κ·Έ iλ₯Ό μ‚¬μš©ν•œλ‹€.

const target = 'Is this all there is?';

// 'is' λ¬Έμžμ—΄κ³Ό λ§€μΉ˜ν•˜λŠ” νŒ¨ν„΄. ν”Œλž˜κ·Έ iλ₯Ό μΆ”κ°€ν•˜λ©΄ λŒ€μ†Œλ¬Έμžλ₯Ό κ΅¬λ³„ν•˜μ§€ μ•ŠλŠ”λ‹€.
const regExp = /is/i;

target.match(regExp);
// -> ["Is", index: 0, input: "Is this all there is?", groups: undefined]

검색 λŒ€μƒ λ¬Έμžμ—΄ λ‚΄μ—μ„œ νŒ¨ν„΄κ³Ό λ§€μΉ˜ν•˜λŠ” λͺ¨λ“  λ¬Έμžμ—΄μ„ μ „μ—­ κ²€μƒ‰ν•˜λ €λ©΄ ν”Œλž˜κ·Έ gλ₯Ό μ‚¬μš©ν•œλ‹€.

const target = 'Is this all there is?';

// 'is' λ¬Έμžμ—΄κ³Ό λ§€μΉ˜ν•˜λŠ” νŒ¨ν„΄.
// ν”Œλž˜κ·Έ gλ₯Ό μΆ”κ°€ν•˜λ©΄ λŒ€μƒ λ¬Έμžμ—΄ λ‚΄μ—μ„œ νŒ¨ν„΄κ³Ό μΌμΉ˜ν•˜λŠ” λͺ¨λ“  λ¬Έμžμ—΄μ„ μ „μ—­ κ²€μƒ‰ν•œλ‹€.
const regExp = /is/ig;

target.match(regExp); // -> ["Is", "is", "is"]

βœ” 31.5.2 μž„μ˜μ˜ λ¬Έμžμ—΄ 검색

. 은 μž„μ˜μ˜ 문자 ν•œ 개λ₯Ό μ˜λ―Έν•œλ‹€. 문자의 λ‚΄μš©μ€ 무엇이든 상관없닀. λ‹€μŒ 예제의 경우 .을 3개 μ—°μ†ν•˜μ—¬ νŒ¨ν„΄μ„ μƒμ„±ν–ˆμœΌλ―€λ‘œ 문자의 λ‚΄μš©κ³Ό 상관없이 3자리 λ¬Έμžμ—΄κ³Ό λ§€μΉ˜ν•œλ‹€.

const target = 'Is this all there is?';

// μž„μ˜μ˜ 3자리 λ¬Έμžμ—΄μ„ λŒ€μ†Œλ¬Έμžλ₯Ό κ΅¬λ³„ν•˜μ—¬ μ „μ—­ κ²€μƒ‰ν•œλ‹€.
const regExp = /.../g;

target.match(regExp); // -> ["Is ", "thi", "s a", "ll ", "the", "re ", "is?"]

βœ” 31.5.3 반볡 검색

{m, n}은 μ•žμ„  νŒ¨ν„΄(λ‹€μŒ 예제의 경우 A)이 μ΅œμ†Œ m번, μ΅œλŒ€ n번 λ°˜λ³΅λ˜λŠ” λ¬Έμžμ—΄μ„ μ˜λ―Έν•œλ‹€. 콀마 뒀에 곡백이 있으면 정상 λ™μž‘ν•˜μ§€ μ•ŠλŠ”λ‹€.

const target = 'A AA B BB Aa Bb AAA';

// 'A'κ°€ μ΅œμ†Œ 1번, μ΅œλŒ€ 2번 λ°˜λ³΅λ˜λŠ” λ¬Έμžμ—΄μ„ μ „μ—­ κ²€μƒ‰ν•œλ‹€.
const regExp = /A{1,2}/g;

target.match(regExp); // -> ["A", "AA", "A", "AA", "A"]

{n}은 μ•žμ„  νŒ¨ν„΄μ΄ n번 λ°˜λ³΅λ˜λŠ” λ¬Έμžμ—΄μ„ μ˜λ―Έν•œλ‹€. 즉, {n}은 {n, n}κ³Ό κ°™λ‹€.

const target = 'A AA B BB Aa Bb AAA';

// 'A'κ°€ 2번 λ°˜λ³΅λ˜λŠ” λ¬Έμžμ—΄μ„ μ „μ—­ κ²€μƒ‰ν•œλ‹€.
const regExp = /A{2}/g;

target.match(regExp); // -> ["AA", "AA"]

{n,}은 μ•žμ„  νŒ¨ν„΄μ΄ μ΅œμ†Œ n번 이상 λ°˜λ³΅λ˜λŠ” λ¬Έμžμ—΄μ„ μ˜λ―Έν•œλ‹€.

const target = 'A AA B BB Aa Bb AAA';

// 'A'κ°€ μ΅œμ†Œ 2번 이상 λ°˜λ³΅λ˜λŠ” λ¬Έμžμ—΄μ„ μ „μ—­ κ²€μƒ‰ν•œλ‹€.
const regExp = /A{2,}/g;

target.match(regExp); // -> ["AA", "AAA"]

+λŠ” μ•žμ„  νŒ¨ν„΄μ΄ μ΅œμ†Œ ν•œλ²ˆ 이상 λ°˜λ³΅λ˜λŠ” λ¬Έμžμ—΄μ„ μ˜λ―Έν•œλ‹€. 즉, +λŠ” {1, }κ³Ό κ°™λ‹€.

const target = 'A AA B BB Aa Bb AAA';

// 'A'κ°€ μ΅œμ†Œ ν•œ 번 이상 λ°˜λ³΅λ˜λŠ” λ¬Έμžμ—΄('A, 'AA', 'AAA', ...)을 μ „μ—­ κ²€μƒ‰ν•œλ‹€.
const regExp = /A+/g;

target.match(regExp); // -> ["A", "AA", "A", "AAA"]

?λŠ” μ•žμ„  νŒ¨ν„΄μ΄ μ΅œλŒ€ ν•œ 번(0번 포함)이상 λ°˜λ³΅λ˜λŠ” λ¬Έμžμ—΄μ„ μ˜λ―Έν•œλ‹€. 즉, ?λŠ” {0, 1}κ³Ό κ°™λ‹€.

const target = 'color colour';

// 'colo' λ‹€μŒ 'u'κ°€ μ΅œλŒ€ ν•œ 번(0번 포함) 이상 반볡되고 'r'이 μ΄μ–΄μ§€λŠ” λ¬Έμžμ—΄ 'color', 'colour'λ₯Ό μ „μ—­ κ²€μƒ‰ν•œλ‹€.
const regExp = /colou?r/g;

target.match(regExp); // -> ["color", "colour"]

βœ” 31.5.4 OR 검색

|은 or의 의미λ₯Ό κ°–λŠ”λ‹€.

const target = 'A AA B BB Aa Bb';

// 'A' λ˜λŠ” 'B'λ₯Ό μ „μ—­ κ²€μƒ‰ν•œλ‹€.
const regExp = /A|B/g;

target.match(regExp); // -> ["A", "A", "A", "B", "B", "B", "A", "B"]

βœ” 31.5.5 NOT 검색

[ ... ] λ‚΄μ˜ ^은 not의 의미λ₯Ό κ°–λŠ”λ‹€. 예λ₯Ό λ“€μ–΄, [^0-9]λŠ” 숫자λ₯Ό μ œμ™Έν•œ 문자λ₯Ό μ˜λ―Έν•œλ‹€. λ”°λΌμ„œ [0-9]와 같은 의미의 \d와 λ°˜λŒ€λ‘œ λ™μž‘ν•˜λŠ” \DλŠ” [^0~9]와 κ°™κ³ , [A-Za-z0-9_]와 같은 의미의 \w와 λ°˜λŒ€λ‘œ λ™μž‘ν•˜λŠ” \WλŠ” [A-Za-z0-9_]와 κ°™λ‹€.

const target = 'AA BB 12 Aa Bb';

// 숫자λ₯Ό μ œμ™Έν•œ λ¬Έμžμ—΄μ„ μ „μ—­ κ²€μƒ‰ν•œλ‹€.
const regExp = /[^0-9]+/g;

target.match(regExp); // -> ["AA BB ", " Aa Bb"]

βœ” 31.5.6 μ‹œμž‘ μœ„μΉ˜λ‘œ 검색

[ ... ] λ°–μ˜ ^은 λ¬Έμžμ—΄μ˜ μ‹œμž‘μ„ μ˜λ―Έν•œλ‹€. 단, [ ... ] λ‚΄μ˜ ^λŠ” not의 의미λ₯Ό κ°€μ§€λ―€λ‘œ μ£Όμ˜ν•˜κΈ° λ°”λž€λ‹€.

const target = 'https://poiemaweb.com';

// 'https'둜 μ‹œμž‘ν•˜λŠ”μ§€ κ²€μ‚¬ν•œλ‹€.
const regExp = /^https/;

regExp.test(target); // -> true

βœ” 31.5.7 λ§ˆμ§€λ§‰ μœ„μΉ˜λ‘œ 검색

$λŠ” λ¬Έμžμ—΄μ˜ λ§ˆμ§€λ§‰μ„ μ˜λ―Έν•œλ‹€.

const target = 'https://poiemaweb.com';

// 'com'으둜 λλ‚˜λŠ”μ§€ κ²€μ‚¬ν•œλ‹€.
const regExp = /com$/;

regExp.test(target); // -> true

❗ 31.6 자주 μ‚¬μš©ν•˜λŠ” μ •κ·œν‘œν˜„μ‹

βœ” 31.6.1 νŠΉμ • λ‹¨μ–΄λ‘œ μ‹œμž‘ν•˜λŠ”μ§€ 검사

λ‹€μŒ μ˜ˆμ œλŠ” 검색 λŒ€μƒ λ¬Έμžμ—΄μ΄ β€˜http://’ λ˜λŠ” β€˜https://’ 둜 μ‹œμž‘ν•˜λŠ”μ§€ κ²€μ‚¬ν•œλ‹€.

const url = 'https://example.com';

// 'http://' λ˜λŠ” 'https://'둜 μ‹œμž‘ν•˜λŠ”μ§€ κ²€μ‚¬ν•œλ‹€.
/^https?:\/\//.test(url); // -> true
/^(http|https):\/\//.test(url); // -> true

βœ” 31.6.2 νŠΉμ • λ‹¨μ–΄λ‘œ λλ‚˜λŠ”μ§€ 검사

λ‹€μŒ μ˜ˆμ œλŠ” 검색 λŒ€μƒ λ¬Έμžμ—΄μ΄ β€˜htmlβ€™λ‘œ λλ‚˜λŠ”μ§€ κ²€μ‚¬ν•œλ‹€. β€˜$β€™λŠ” λ¬Έμžμ—΄μ˜ λ§ˆμ§€λ§‰μ„ μ˜λ―Έν•œλ‹€.

const fileName = 'index.html';

// 'html'둜 λλ‚˜λŠ”μ§€ κ²€μ‚¬ν•œλ‹€.
/html$/.test(fileName); // -> true

βœ” 31.6.3 숫자둜만 이루어진 λ¬Έμžμ—΄μΈμ§€ 검사

λ‹€μŒ μ˜ˆμ œλŠ” 검색 λŒ€μƒ λ¬Έμžμ—΄μ΄ 숫자둜만 이루어진 λ¬Έμžμ—΄μΈμ§€ κ²€μ‚¬ν•œλ‹€.

const target = '12345';

// 숫자둜만 이루어진 λ¬Έμžμ—΄μΈμ§€ κ²€μ‚¬ν•œλ‹€.
/^\d+$/.test(target); // -> true

βœ” 31.6.4 ν•˜λ‚˜ μ΄μƒμ˜ 곡백으둜 μ‹œμž‘ν•˜λŠ”μ§€ 검사

λ‹€μŒ μ˜ˆμ œλŠ” 검색 λŒ€μƒ λ¬Έμžμ—΄μ΄ ν•˜λ‚˜ μ΄μƒμ˜ 곡백으둜 μ‹œμž‘ν•˜λŠ”μ§€ κ²€μ‚¬ν•œλ‹€.

const target = ' Hi!';

// ν•˜λ‚˜ μ΄μƒμ˜ 곡백으둜 μ‹œμž‘ν•˜λŠ”μ§€ κ²€μ‚¬ν•œλ‹€.
/^[\s]+/.test(target); // -> true

βœ” 31.6.5 μ•„μ΄λ””λ‘œ μ‚¬μš© κ°€λŠ₯ν•œμ§€ 검사

λ‹€μŒ μ˜ˆμ œλŠ” 검색 λŒ€μƒ λ¬Έμžμ—΄μ΄ μ•ŒνŒŒλ²³ λŒ€μ†Œλ¬Έμž λ˜λŠ” 숫자둜 μ‹œμž‘ν•˜κ³  λλ‚˜λ©° 4~10μžλ¦¬μΈμ§€ κ²€μ‚¬ν•œλ‹€.

const id = 'abc123';

// μ•ŒνŒŒλ²³ λŒ€μ†Œλ¬Έμž λ˜λŠ” 숫자둜 μ‹œμž‘ν•˜κ³  λλ‚˜λ©° 4 ~ 10μžλ¦¬μΈμ§€ κ²€μ‚¬ν•œλ‹€.
/^[A-Za-z0-9]{4,10}$/.test(id); // -> true

βœ” 31.6.6 메일 μ£Όμ†Œ ν˜•μ‹μ— λ§žλŠ”μ§€ 검사

λ‹€μŒ μ˜ˆμ œλŠ” 검색 λŒ€μƒ λ¬Έμžμ—΄μ΄ 메일 μ£Όμ†Œμ˜ ν˜•μ‹μ— λ§žλŠ”μ§€ κ²€μ‚¬ν•œλ‹€.

const email = 'ungmo2@gmail.com';

/^[0-9a-zA-Z]([-_\.]?[0-9a-zA-Z])*@[0-9a-zA-Z]([-_\.]?[0-9a-zA-Z])*\.[a-zA-Z]{2,3}$/.test(email); // -> true

(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])

βœ” 31.6.7 ν•Έλ“œν° 번호 ν˜•μ‹μ— λ§žλŠ”μ§€ 검사

λ‹€μŒ μ˜ˆμ œλŠ” 검색 λŒ€μƒ λ¬Έμžμ—΄μ΄ ν•Έλ“œν° 번호 ν˜•μ‹μ— λ§žλŠ”μ§€ κ²€μ‚¬ν•œλ‹€.

const cellphone = '010-1234-5678';

/^\d{3}-\d{3,4}-\d{4}$/.test(cellphone); // -> true

βœ” 31.6.8 특수 문자 포함 μ—¬λΆ€ 검사

λ‹€μŒ μ˜ˆμ œλŠ” 검색 λŒ€μƒ λ¬Έμžμ—΄μ— 특수 λ¬Έμžκ°€ ν¬ν•¨λ˜μ–΄ μžˆλŠ”μ§€ κ²€μ‚¬ν•œλ‹€.

const target = 'abc#123';

// A-Za-z0-9 μ΄μ™Έμ˜ λ¬Έμžκ°€ μžˆλŠ”μ§€ κ²€μ‚¬ν•œλ‹€.
(/[^A-Za-z0-9]/gi).test(target); // -> true
profile
μ΄μ‚¬μ€‘μž…λ‹ˆλ‹€!🌟https://velog.io/@devkyoung2

0개의 λŒ“κΈ€

κ΄€λ ¨ μ±„μš© 정보