정규표현식 Regular Expression

2022. 1. 20. 20:19카테고리 없음

정규표현식, 줄여서 정규식은 웹에서 클라이언트가 어떠한 expression에 합당한 값을 입력했는지 검사할 때 많이 쓴다.

여러 언어에서 쓸 수 있지만, 나는 이번에는 javascript에서 쓰기로 한다.

 

일단 내가 좋아하는 mozila Doc

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions

 

Regular expressions - JavaScript | MDN

Regular expressions are patterns used to match character combinations in strings. In JavaScript, regular expressions are also objects. These patterns are used with the exec() and test() methods of RegExp, and with the match(), matchAll(), replace(), replac

developer.mozilla.org


You construct a regular expression in one of two ways: 

정규식은 두가지 방법으로 만들 수 있다.

  • Using a regular expression literal, which consists of a pattern enclosed between slashes, as follows: 리터럴
  let re = /ab+c/;

Regular expression literals provide compilation of the regular expression when the script is loaded. If the regular expression remains constant, using this can improve performance.

정규식 literal은 스크립트를 불러올때 컴파일된다. 정규식이 constant로 남아있으면 성능이 좋아짐~

 

  • Or calling the constructor function of the RegExp object, as follows: RegExp 객체 생성자로 만들기
  let re = new RegExp('ab+c');

Using the constructor function provides runtime compilation of the regular expression. Use the constructor function when you know the regular expression pattern will be changing, or you don't know the pattern and are getting it from another source, such as user input.

생성자함수를 쓰면 런타임 컴파일이 된다.

생성자함수를 쓰는경우: 정규식 패턴이 바뀔 때, 또는 패턴이 아직 안 정해졌고 사용자가 입력한다든지 할 때


생활코딩 자바스크립트 - 정규표현식 강의가 무척 짧아서

(한 강의당 10분 내외인데 7강임.. 하지만 생코는 1.5배속이 기본!!)

휘리릭 봤는데 확실히 간단히 보고서 모질라를 보니깐 이해가 잘 된다~

 

나는 일단은 글쓰기 화면에서 yyyy-mm-dd형식으로 날짜를 입력받아야하는 거라서, literal을 쓰면 된다.