Java Script & DOM/Java Script

정규표현(Regular Expression) 패턴(Pattern) 지정 방법

본클라쓰 2010. 12. 15. 20:26

 

정규표현을 사용하여 패턴을 지정할 때는 다음과 같은 방법으로 지정한다.

 

 

1. 괄호(Bracket)

표현식

설 명

[abc]

괄호 안에 있는 문자열을 찾음(각각 하나씩 모든 문자열을 반환)

[^abc]

괄호 안에 있는 문자열이 아닌 것을 찾음

[0-9]

0부터 9까지 있는 문자열을 모두 찾음

[A-Z]

대문자 A부터 대문자 Z까지 있는 모든 문자

[a-z]

소문자 a부터 소문자 z까지

[A-z]

대문자 A부터 소문자 z까지

(red|blue|green)

지정한 문자열을 찾음(해당하는 문자는 모두 반환)

 

 

괄호 사용예)

var str = "Is this all there is?";

var patt = /[ab]/g;

document.write( str.match( patt ) );

 

결과) s,s,a,s

 

   

patt = /[a-k]/g;

document.write( str.match( patt ) );

 

결과) h,i,a,h,e,e,i

   

 

patt = /(a|t)/g;

document.write( str.match( patt ) );

 

결과) t,a,t

 

 

2. Metacharacters(메타 문자들) 

메타 문자

설명

.

하나의 문자를 나타내며 새로운 라인이나 라인 터미네이터는 제외한다.

\w

문자만 찾는다

\W

문자가 아닌것만 찾는다.

\d

0-9까지의 값만 찾는다.

\D

0-9가 아닌 문자만 찾는다.

\s

공백문자를 찾는다. 공백은 space, tab, carriage return, new line, vertical tab 등이다.

\S

공백이 아닌 문자를 찾는다.

\o

null 문자를 찾는다.

\n

new line 문자를 찾는다.

\xxx

해당 숫자(아스키값)에 해당하는 문자를 찾는다. ex) \127은 W

 

 

메타문자 사용예)

var str = "That's hot!";

var patt = /h.t/g;

document.write( str.match( patt ) );

 

결과) hat,hot

 

 

var str = "Give 100%";

var patt = /\w/g;

document.write( str.match( patt ) );

 

결과) G,i,v,e,1,0,0

 

 

var str = "Give 100%";

var patt = /\d/g;

document.write( str.match( patt ) );

 

결과) 1,0,0

 

 

var str = "Is this all there is?";

var patt = /\s/g;

document.write( str.match( patt ) );

 

결과) , , ,

 

 

var str = "Visit W3Schools";

var patt = /\bW3/g;

document.write( str.match( patt ) );

 

결과) W3

 

 

3. 한정기호(Quantifiers)

한정기호

설 명

n+

n문자를 적어도 한 개 이상 포함한 문자열을 모두 찾는다.

n*

n문자가 있거나 없는 경우를 모두 찾는다.

n?

n문자가 없거나 한번 있는 경우를 모두 찾는다.

n{x}

n문자나 수식어구를 사용하여 해당 문자의 수가 일치하는 문자열을 찾는다.

n{x,y}

n{x}와 같은 작용을 하는데 차이점은 x의 수나 y와 일치하는 문자열을 찾는다.

n{x,}

n{x}와 같은 작용을 하는데 차이점은 최소 x의 수만큼만 일치하면 모두 찾는다.

n$

n문자로 끝나는 맨 마지막 문자열을 찾는다.

^n

n문자로 시작하는 맨 처음 문자열을 찾는다

?=n

n문자가 뒤에 오는 문자열을 찾는다.

?!n

n문자가 뒤에 오지 않는 문자열을 찾는다.

 

 

 

 

한정기호 사용예)

var str = "Hello world! Hello!";

var patt = /o+/g;

document.write( str.match( patt ) );

 

결과) o,o,o

 

 

var str = "Hello world! Hello";

vat patt = /o*/g;

document.write( str.match( patt ) );

 

결과) ,,,,o,,o,,,,,,,,o,

 

 

var str = "Hello world! Hello";

vat patt = /lo*/g;

document.write( str.match( patt ) );

 

결과) l,lo,l,l,lo

 

 

var str = "1, 100 or 1000?";

var patt = /10?/g;

document.write( str.match( patt ) );

 

결과) 1, 10, 10

 

 

var str = "100, 1000 or 10000, 1111?";

var patt = /1{4}/g;

 

패턴이 위와 같을 경우 결과는 1111

패턴이 /\d{4}/g 일 경우에는 1000,1000,1111

 

 

var str = "100, 1000 or 10000?";

var patt = /\d{3,}/g;

document.write( str.match( patt ) );

 

결과) 100, 1000, 10000

 

 

var str = "Is this his";

var patt = /is$/g;

document.write( str.match( patt ) );

 

결과) is

 

 

 

var str = "Is this all there is";

var patt = /is(?= all)/;

document.write( str.match( patt ) );

 

결과) is

만약, 패턴은 /is(?=all)/ 이렇게 변경하면 null이 반환된다. 문자열은 공백을 포함한다.

 

 

var str = "Is this all there is";

var patt = /is(?! all)/gi;

document.write( str.match( patt ) );

 

결과) Is, is