JavaScript 정규표현식(Regular Expressions)


정규표현식(regular expression)은 검색 패턴(search pattern)을 형성하는 일련의 문자입니다.

텍스트 검색 및 텍스트 바꾸기 작업에서 검색 패턴을 사용할 수 있습니다.


정규 표현식(Regular Expression)이란?

정규 표현식은 검색 패턴(search pattern)을 형성하는 일련의 문자들이다.

당신은 텍스트에서 데이터를 검색 할 때, 당신이 찾고 있는 것을 설명하기 위해서 이 검색 패턴을 사용할 수 있습니다.

정규 표현식은 하나의 문자일 수도, 또는 더 복잡한 패턴이 될 수 있습니다.

정규 표현식은 모든 종류의 텍스트 검색 텍스트 교체 작업을 수행하는 데 사용할 수 있다 .

Syntax

/pattern/modifiers;

Example

var patt = /w3schools/i;

Example explained:

/w3schools/i   정규 표현식 (다음과 같이 변수에 할당된  var patt =).

w3schools  패턴 (검색에 사용 될).

i   수정자(modifier) (검색임 대소문자 구분하지않도록(case insensitive) 수정한다.).


String Methods 사용

JavaScript 에서 정규 표현식은 종종 string methods 와 사용된다 : search() 와 replace()

search() method   에서는 일치를 검색하는데 표현식을 사용하며, 일치하는 위치를 반환합니다.

replace() method   는 패턴이 대체된 곳의 수정된 문자열을 반환한다.


Using String search(), With a Regular Expression

Example

Use a regular expression to do a case-insensitive search for "w3schools" in a string:

var str = "Visit W3Schools";
var n = str.search(/w3schools/i);

The result in n will be:

6
Try it Yourself »


Using String search(), With a String

Since search() is a string method, it will accept a string as the search argument:

Example

Use a string to do a search for "W3schools" in a string:

var str = "Visit W3Schools!";
var n = str.search("W3Schools");
Try it Yourself »


Use String replace(), With a Regular Expression

Example

Use a case insensitive regular expression to replace Microsoft with W3Schools in a string:

var str = "Visit Microsoft!";
var res = str.replace(/microsoft/i, "W3Schools");

The result in res will be:

Visit W3Schools!
Try it Yourself »

Using String replace(), With a String

Since replace() is a string method, it will also accept a string as the search argument:

var str = "Visit Microsoft!";
var res = str.replace("Microsoft", "W3Schools");
Try it Yourself »


Did You Notice?


Note   위의 메서드에서 (string 인수 대신에) 정규표현식 인수가 사용될 수 있다.
  정규표현식은 검색을 훨씬 더 강력하게 (예를 들면 대소문자 구분하지 않음) 할 수 있다.


Regular Expression 수정자(Modifiers)

수정자(Modifiers)는  대소 문자를 구분하지 않는(case-insensitive) 더 많은 글로벌 검색을 수행하는데 사용될 수 있습니다:

Modifier Description
i Perform case-insensitive matching
g Perform a global match (find all matches rather than stopping after the first match)
m Perform multiline matching

Regular Expression Patterns

Brackets 는 문자의 범위를 지정하는데 사용됩니다 :

Expression Description
[abc] Find any of the characters between the brackets
[0-9] Find any of the digits between the brackets
(x|y) Find any of the alternatives separated with |
Metacharacters 는 특별한 의미를 갖는 문자입니다:

Metacharacter Description
\d Find a digit
\s Find a whitespace character
\b Find a match at the beginning or at the end of a word
\uxxxx Find the Unicode character specified by the hexadecimal number xxxx

Quantifiers 는 수량을 정의한다:

Quantifier Description
n+ Matches any string that contains at least one n
n* Matches any string that contains zero or more occurrences of n
n? Matches any string that contains zero or one occurrences of n


Using the RegExp Object

JavaScript 에서 RegExp 객체는 미리 정의된 속성(properties)과 메서드(methods)를 갖는 정규표현식 객체입니다.


test() 사용

test() method 는 RegExp 표현식의 메서드(method) 이다.

패턴에 대하여 문자열을 검색하고, 결과에 따라서 true 또는 false 를 반환한다.

다음 예는 문자 "e"에 대하여 문자열을 검색한다.:

Example

var patt = /e/;
patt.test("The best things in life are free!");

Since there is an "e" in the string, the output of the code above will be:

true
Try it Yourself »

You don't have to put the regular expression in a variable first. The two lines above can be shortened to one:

/e/.test("The best things in life are free!");


exec() 사용

exec() method 는 RegExp 표현식의 메서드(method) 이다.

특정 패턴에 대하여 문자열을 검색하고, 찾은 문자를를 반환한다.

일치하는 항목이 없는 경우에는 null 을 반환한다.

다음 예는 문자 "e"에 대하여 문자열을 검색한다.:

Example 1

/e/.exec("The best things in life are free!");

Since there is an "e" in the string, the output of the code above will be:

e
Try it Yourself »


Complete RegExp Reference

 Complete JavaScript RegExp Reference.