31장 RegExp
1. 정규표현식이란?
- 일정한 패턴을 가진 문자열의 집합을 표현하기 위해 사용하는 형식 언어
- JS 고유 문법이 아니며, JS는 Perl의 정규 표현식 문법을 사용
- 문자열을 대상으로 패턴 매칭 기능을 제공
const tel = '010-1234-567팔';
const regExp = /^\d{3}-\d{4}-\d{4}$/;
regExp.test(tel);
2. 정규 표현식의 생성
2.1 정규 표현식 리터럴
const target = 'Is this all there is?';
const regexp = /is/i;
regexp.test(target);
2.2 RegExp 생성자 함수
new RegExp(pattern[, flags]);
const target = 'Is this all there is?';
const regexp = new RegExp(/is/i);
regexp.test(target);
- 변수를 사용해 동적으로 RegExp 객체 생성 가능
const count = (str, char) => (str.match(new RegExp(char, 'gi')) ?? []).length;
count('Is this all there is?', 'is');
count('Is this all there is?', 'xx');
3. RegExp 메서드
3.1 regExp.prototype.exec
- 인자로 전달받은 문자열에 대해 정규 표현식의 패턴 검색하여 첫번째 매칭 결과를 배열로 반환
const target = 'Is this all there is?';
const regExp = /is/;
target.match(regExp);
3.2 regExp.prototype.test
- 인자로 전달받은 문자열에 대해 정규 표현식의 패턴 검색하여 매칭 결과를 boolean으로 반환
const target = 'Is this all there is?';
const regExp = /is/;
target.test(regExp);
3.2 regExp.prototype.match
- 대상 문자열과 인수로 전달받은 정규 표현식과의 매칭 결과를 배열로 반환
const target = 'Is this all there is?';
const regExp = /is/;
target.match(regExp);
- exec : g 플래그를 지정해도 첫 번째 매칭 결과만 반환
- match : g 플래스 지정 시, 모든 매칭 결과를 배열로 반환
const target = 'Is this all there is?';
const regExp = /is/g;
target.match(regExp);
4. 플래그
- 정규 표현식의 검색 방식을 설정하기 위해 사용되는 6가지 플래그
- 다음은 주로 사용되는 3개 플래그
| 플래그 | 의미 | 설명 |
|---|
| i | Ignore case | 대소문자를 구별하지 않고 패턴을 검색한다. |
| g | Global | 대상 문자열 내에서 패턴과 일치하는 모든 문자열을 전역 검색한다. |
| m | Multi line | 문자열의 행이 바뀌더라도 패턴 검색을 계속한다. |
const target = 'Is this all there is?';
target.match(/is/);
target.match(/is/i);
target.match(/is/g);
target.match(/is/ig);
5. 패턴
5.1 문자열 검색
const target = 'Is this all there is?';
const regExp = /is/ig;
target.match(regExp);
5.2 임의의 문자열 검색
const target = 'Is this all there is?';
const regExp = /.../g;
target.match(regExp);
5.3 반복 검색
- {m,n} : 최소 m번, 최대 n번 반복되는 문자열을 의미
- 위 패턴에 공백 넣으면 동작 안함
const target = 'A AA B BB Aa Bb AAA';
const regExp = /A{1,2}/g;
target.match(regExp);
regExp = /A{2}/g;
regExp = /A{2,}/g;
regExp = /A+/g;
- ?는 패턴이 최대 한 번 이상 반복되는 문자열을 의미 (={0,1}}
const target = 'color colour';
const regExp = /colou?r/g;
target.match(regExp);
5.4 OR 검색
const target = 'A AA B BB Aa Bb';
const regExp = /A|B/g;
target.match(regExp);
- [] 내의 문자는 or로 동작
- -를 이용해 범위 지정 가능
const target = 'AA BB Aa Bb';
const regExp = /[A-Za-z]+/g;
target.match(regExp);
5.5 NOT 검색
const target = 'AA BB 12 Aa Bb';
const regExp = /[^0-9]+/g;
target.match(regExp);
5.6 시작 위치로 검색
const target = 'https://www.google.com';
const regExp = /^https/;
target.test(regExp);
5.7 마지막 위치로 검색
const target = 'https://www.google.com';
const regExp = /com$/;
target.test(regExp);
6. 자주 사용하는 정규표현식
6.1 특정 단어로 시작하는지 검사
const url = 'https://example.com';
/^https?:\/\//.test(url);
/^(http|https):\/\//.test(url);
6.2 특정 단어로 끝나는지 검사
const fileName = 'index.html';
/html$/.test(fileName);
6.3 숫자로만 이뤄진 문자열인지 검사
const target= '12345';
/^\d+$/.test(target);
6.4 하나 이상의 공백으로 시작하는지 검사
const target= ' Hi!';
/^[\s]+/.test(target);
6.5 아이디로 사용 가능한지 검사
- 알파벳 대소문자 또는 숫자로 시작하고 끝나야 한다.
- 4~10자리인지 검사
const id = 'abc123';
/^[A-Za-z0-9]{4,10}$/.test(id);
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);
6.7 핸드폰 번호 형식에 맞는지 검사
const cellphone = '010-1234-5678';
/^\d{3}-\d{3,4}-\d{4}$/.test(cellphone);
6.8 특수 문자 포함 여부 검사
const target = 'abc#123';
(/[^A-Za-z0-9]/gi).test(target);
(/[\{\}\[\]\/?.,;:|\)*~`!^\-_+<>@\#$%^\\\\=\(\'\"]/gi).test(target);
target.replace(/[^A-Za-z0-9]/gi, '');
긁어온 거
/^[a-z0-9_-]{2,10}$/
/^[0-9-]{19}$/
/^[0-9]{4}[-\s\.]?[0-9]{4}[-\s\.]?[0-9]{4}[-\s\.]?[0-9]{4}$/
/[a-zA-Z0-9]/
/^[(]?[0-9]{3}[)]?[-\s\.]?[0-9]{3,4}[-\s\.]?[0-9]{4}$/
/^\d{3}-\d{3,4}-\d{4}$/
/(^02.{0}|^01.{1}|[0-9]{3})([0-9]+)([0-9]{4})/
/^01(?:0|1|[6-9])[.-]?(\\d{3}|\\d{4})[.-]?(\\d{4})$/
/^[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}$/
/[ -~]/
/^[a-fA-F0-9]{2}(:[a-fA-F0-9]{2}){5}$/
/(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}/
/(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3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