본문 바로가기

카테고리 없음

자바스크립트 정규식 JavaScript Regular Expressions

728x90

정규식 특수문자

\ escape 

/a*/ 0이상의 'a'들

/a\*/ 문자열 'a*' 같은 문자와 일치


^ 입력의 시작

/^A/ "an A"의 'A'에 일치하지 않음, "An E"의 'A'에서는 일치

문자셋([abc]) 패턴의 첫글자는 다른 의미??


$ 입력의 끝

/t$/ "eater"의 't'에는 일치하지 않음. "eat"에서는 일치


x* 0회 이상 연속으로 반복되는 문자 x {0,}와 동일

/bo*/는 "A ghost booooed"의 'boooo'에 일치, "A bird warbled"의 'b'에 일치, "A goat grunted" 불일치


x+ 1회 이상 연속으로 반복되는 문자 x {1,}와 동일

/a+/ "candy"의 'a'에 일치, "caaaaaaandy"의 모든 'a'들에 일치, "cndy" 불일치



x? 문자x가 0 또는 1회 반복 {0,1}와 동일

/e?le?/ "angel"의 'el'에 일치, "angle"의 'le'에 일치, "oslo"의 'l' 일치

수량자 *,+,?,{} 바로 뒤에 사용하면 가장 적은 문자들에 일치

/\d+/ "123abc" "123"이 일치

/\d+?/ "1"만 일치

??이표시작부분의x(?=y)와 x(?!y)에서 설명된 것처럼 사전 검증에서도 쓰임


.x x가 마지막으로 끝남

/.n/ "nay, an apple is on the thee"에 'an'과 'on' 일치 'nay' 불일치


(x) 'x'에 일치, 일치한 것을 기억. 괄호는 포획괄호(capturing parentheses)


(?:x) 'x'에 일치, 일치한 것을 기억하지 않음. 괄호는 비포획 괄호(non-capturing parentheses)


x(?=y) 오직 'y'가 뒤따라오는 'x'에만 일치 lookahead

/Jack(?=Sparat)/ 'Sprat'가 뒤따라오는 'Jack'에만 일치

/Jack(?=Sprat|Frost)/는 'Sprat' 또는 'Frost'가 뒤따라오는 'Jack'에만 일치.  'Sprat' 및 'Frost'는 일치 결과의 일부가 아님.



https://developer.mozilla.org/ko/docs/Web/JavaScript/Guide/%EC%A0%95%EA%B7%9C%EC%8B%9D



Groups & Lookaround

(abc) capture group

\1 backreference to group #1

(?:abc) non-capturing group

(?=abc) positive lookahead

(?!abc) negative lookahead


Metacharacters


\W Find a non-word character


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



- at least 8 characters
- must contain at least 1 uppercase letter, 1 lowercase letter, and 1 number
- Can contain special characters


/^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[a-zA-Z]).{8,}$/gm



jsredW special characters


728x90