[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ko/docs/Web/JavaScript/Guide/Regular_expressions [Back]  [Original]

- JavaScript | MDN

This page was translated from English by the community. Learn more and join the MDN Web Docs community.

View in English Always switch to English

, . JavaScript , RegExp exec() test() . String match(), matchAll(), replace(), replaceAll(), search(), split() . JavaScript .

In this article

.

  • . .

    js
    const re = /ab+c/;
    

    , .

  • RegExp .

    js
    const re = new RegExp("ab+c");
    

    . , .

/abc/ , /ab+c/ /Chapter (\d+)\.\d*/ . (\d+) , . .

. , /abc/ "abc" . "Hi, do you know your abc's?" "The latest airplane designs evolved from slabcraft." , "abc" . "Grab crab" , "ab c" , "abc" .

"b" . " "a" 0 "b", "c"" /ab*c/ . "b" * " 0 " . "cbbabbbbcdebc" , "abbbbc" .

, .

, (, , ) .

.

.

.

/, , , .

.

/
\, ., \cX, \d, \D, \f, \n, \r, \s, \S, \t, \v, \w, \W, \0, \xhh, \uhhhh, \uhhhhh, [\b]

^, $, x(?=y), x(?!y), (?<=y)x, (?<!y)x, \b, \B

(x), (?:x), (?<Name>x), x|y, [xyz], [^xyz], \Number

*, +, ?, x{n}, x{n,}, x{n,m}

\p{UnicodeProperty}, \P{UnicodeProperty}

: , .

("*" ) , (\) . "a" ("*") "b" /a\*b/ . "*" "", .

, (/) . . "/example/" /\/example\/[a-z]/ . .

. "A:\", "B:\", "C:\", ..., "Z:\" /[A-Z]:\\/. , .

RegExp , . . , /a\*b/ new RegExp("a\\*b") .

String.replace .

js
function escapeRegExp(string) {
  return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); // $&    
}

"g" . .

" escapeRegExp() JavaScript ?" TC39 .

, . . .

JavaScript

RegExp test() exec(), String match(), replace(), search(), split() .

exec() . , null .
test() . true false .
match() . null .
matchAll() .
search() . , -1 .
replace() , .
replaceAll() , .
split() .

test() search() . exec() match() . , exec() match() , . null . (null false )

, exec() .

js
const myRe = /d(b+)d/g;
const myArray = myRe.exec("cdbbdbsbz");

.

js
const myArray = /d(b+)d/g.exec("cdbbdbsbz");
// 'cdbbdbsbz'.match(/d(b+)d/g);  ,
// 'cdbbdbsbz'.match(/d(b+)d/g)   [ 'dbbd' ] 
// /d(b+)d/g.exec('cdbbdbsbz')   [ 'dbbd', 'bb', index: 1, input: 'cdbbdbsbz' ]

( exec() )

.

js
const myRe = new RegExp("d(b+)d", "g");
const myArray = myRe.exec("cdbbdbsbz");

, .

myArray . ['dbbd', 'bb', index: 1, input: 'cdbbdbsbz']
index . (0 ) 1
input . 'cdbbdbsbz'
[0] . 'dbbd'
myRe lastIndex . (g . ) 5
source . . . 'd(b+)d'

, . , , . .

js
const myRe = /d(b+)d/g;
const myArray = myRe.exec("cdbbdbsbz");
console.log(`lastIndex  ${myRe.lastIndex}`);

// "lastIndex  5"

...

js
const myArray = /d(b+)d/g.exec("cdbbdbsbz");
console.log(`lastIndex  ${/d(b+)d/g.lastIndex}`);

// "lastIndex  0"

/d(b+)d/g lastIndex . , .

. , .

d . RegExp.prototype.hasIndices
g . RegExp.prototype.global
i . RegExp.prototype.ignoreCase
m . RegExp.prototype.multiline
s . . RegExp.prototype.dotAll
u "unicode", . RegExp.prototype.unicode
y "" , . sticky . RegExp.prototype.sticky

.

js
const re = /pattern/flags;

.

js
const re = new RegExp("pattern", "flags");

.

, re = /\w+\s/g , .

js
const re = /\w+\s/g;
const str = "fee fi fo fum";
const myArray = str.match(re);
console.log(myArray);

// ["fee ", "fi ", "fo "]

...

js
const re = /\w+\s/g;

.

js
const re = new RegExp("\\w+\\s", "g");

.

m . , m , ^ $ , .

exec()

RegExp.prototype.exec() g , .

js
const str = "fee fi fo fum";
const re = /\w+\s/g;

console.log(re.exec(str)); // ["fee ", index: 0, input: "fee fi fo fum"]
console.log(re.exec(str)); // ["fi ", index: 4, input: "fee fi fo fum"]
console.log(re.exec(str)); // ["fo ", index: 7, input: "fee fi fo fum"]
console.log(re.exec(str)); // null

, String.prototype.match() , .

js
console.log(str.match(re)); // ["fee ", "fi ", "fo "]

. "" . , , . , .

.

  1. (^)
  2. ((?:)), 1 (\d{3}), (|), (\() (\d{3}) (\))
  3. (()), 2 , ,
  4. 3 (\d{4})
  5. 4 , (\1)
  6. 5 (\d{4})
  7. ($)

HTML

html
<p>
     ""  .
  <br />
  ###-####-####  .
</p>
<form id="form">
  <input id="phone" />
  <button type="submit"></button>
</form>
<p id="output"></p>

JavaScript

js
const form = document.querySelector("#form");
const input = document.querySelector("#phone");
const output = document.querySelector("#output");

const re = /^(?:\d{3}|\(\d{3}\))([-\/\.])\d{4}\1\d{4}$/;

function testInfo(phoneInput) {
  const ok = re.exec(phoneInput.value);

  if (!ok) {
    output.textContent = `   . (${phoneInput.value})`;
  } else {
    output.textContent = `.  ${ok[0]} .`;
  }
}

form.addEventListener("submit", (event) => {
  event.preventDefault();
  testInfo(input);
});

RegExr

, , .

Regex tester

/.

Regex visualizer

.


Web Proxy Viewer  |  New URL  |  Original Page