본문 바로가기
정리_창고

JavaScript - 정규표현식

by SKim입니다 2020. 7. 8.

** 목차 **

 

 

참고   https://act-think.tistory.com/191?category=868707

 

정리_정규표현식

※ greedy와 ungreedy의 차이 - ↓ 이 글 2/3 지점 쯤에 댓글 캡쳐 https://act-think.tistory.com/63 5/26 생활코딩 * 오늘의 진도  정규표현식 정규표현식 패턴들 정규표현식: 문자를 처리하기 위한 일종..

act-think.tistory.com

정규표현식: 문자열에서 문자 조합을 매치하기 위해 사용되는 패턴들

- RegExp의 exec()와 test() 메소드, 그리고

- String의 match(), matchAll(), replace(), replaceAll(), search(), split() 메소드와 함께 쓰인다.

자바스크립트에서는 정규표현식도 object이다.

 

정규표현식을 만들기(create = construct)

1. 정규표현식 literal(슬래쉬) 사용

  let re = /ab+c/;

 정규식이 스크립트를 로드할 때 컴파일된다.

 → 정규식이 상수일 때 사용하면 성능을 향상시킬 수 있다.

 

2. RegExp object의 constructor 함수를 호출

  let re = new RegExp('ab+c');

 정규식이 런타임으로(= 실행될 때) 컴파일된다.

 → 정규식 패턴이 변경될 예정이거나, 

 user input과 같이 다른 소스에서 받아올 예정이어서 아직 모를 때 사용한다.

 

 

 

정규표현식 패턴을 작성하기(write)

 정규식 패턴은 단순 문자들로 구성될 수도 있고 ex) /abc/

 단순 + 특수 문자들의 조합일 수도 있다. ex) /ab*c/,  /Chapter (\d+)\.\d*/.

 괄호는 memory device(기억장치)처럼 쓰인다. = 나중에 사용되기 위해서 저장된다.

 

1. 단순 패턴 사용하기

 simple 패턴은 문자열을 있는 그대로(모든 문자들이 그 순서대로 있어야 한다.) 매치시킬 때 사용한다. 

 ex) /abc/ → abc's (O)   slabcraft (O)   Grab crab (X)

 

2. 특수 문자 사용하기

 

 Unicode property escapes                                                                                  

 

Unicode property escapes

Unicode property escapes Regular Expressions allows for matching characters based on their Unicode properties. A character is described by several properties which are either binary ("boolean-like") or non-binary. For instance, unicode property escapes can

developer.mozilla.org

 

※ Groups and ranges에서 괄호로 문자열을 기억하기 예제

 

 

Groups and ranges

Groups and ranges indicate groups and ranges of expression characters.

developer.mozilla.org

 

※ sticky search

 

 

RegExp.prototype.sticky

The sticky property reflects whether or not the search is sticky (searches in strings only from the index indicated by the lastIndex property of this regular expression). sticky is a read-only property of an individual regular expression object.

developer.mozilla.org

 

 

 

자바스크립트에서 정규표현식을 사용하기

RegExp 메소드인 test(), exec()와

String 메소드인 match(), replace(), search(), split()과 함께 쓰인다.

 

 

※ flag 사용하는 방법

var re = /pattern/flags;   // 또는
var re = new RegExp('pattern', 'flags');

flag를 나중에 추가할 수는 없다.

 

Advanced searching with flags

 

 

 

 

 

 

< 출처 >

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

 

Regular expressions

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(), re

developer.mozilla.org

https://www.w3schools.com/jsref/jsref_obj_regexp.asp

 

JavaScript RegExp Reference

JavaScript RegExp Reference RegExp Object A regular expression is an object that describes a pattern of characters. Regular expressions are used to perform pattern-matching and "search-and-replace" functions on text. Syntax Example explained: /w3schools/i�

www.w3schools.com

 

댓글