| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/ko/docs/Web/JavaScript/Guide/Regular_expressions | [Back] [Original] |
Get to know MDN better
This page was translated from English by the community. Learn more and join the MDN Web Docs community.
, . JavaScript , RegExp exec() test() . String match(), matchAll(), replace(), replaceAll(), search(), split() . JavaScript .
.
. .
const re = /ab+c/;
, .
RegExp .
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" .
, .
.
("*" ) , (\) . "a" ("*") "b" /a\*b/ . "*" "", .
, (/) . . "/example/" /\/example\/[a-z]/ . .
. "A:\", "B:\", "C:\", ..., "Z:\" /[A-Z]:\\/. , .
RegExp , . . , /a\*b/ new RegExp("a\\*b") .
function escapeRegExp(string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); // $&
}
" escapeRegExp() JavaScript ?" TC39 .
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() .
const myRe = /d(b+)d/g;
const myArray = myRe.exec("cdbbdbsbz");
.
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() )
.
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' |
, . , , . .
const myRe = /d(b+)d/g;
const myArray = myRe.exec("cdbbdbsbz");
console.log(`lastIndex ${myRe.lastIndex}`);
// "lastIndex 5"
...
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 |
.
const re = /pattern/flags;
.
const re = new RegExp("pattern", "flags");
.
, re = /\w+\s/g , .
const re = /\w+\s/g;
const str = "fee fi fo fum";
const myArray = str.match(re);
console.log(myArray);
// ["fee ", "fi ", "fo "]
...
const re = /\w+\s/g;
.
const re = new RegExp("\\w+\\s", "g");
.
m . , m , ^ $ , .
RegExp.prototype.exec() g , .
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() , .
console.log(str.match(re)); // ["fee ", "fi ", "fo "]
. "" . , , . , .
.
^)(?:)), 1 (\d{3}), (|), (\() (\d{3}) (\))()), 2 , , \d{4})\1)\d{4})$)<p>
"" .
<br />
###-####-#### .
</p>
<form id="form">
<input id="phone" />
<button type="submit"></button>
</form>
<p id="output"></p>
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);
});
, , .
/.
.
This page was last modified on 2025 12 19 by MDN contributors.
Your blueprint for a better internet.
Portions of this content are 19982026 by individual mozilla.org contributors. Content available under a Creative Commons license.
| Web Proxy Viewer | New URL | Original Page |