| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/ru/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 . exec test RegExp match, replace, search, split String. JavaScript.
:
, :
var re = /ab+c/;
. , , .
RegExp, :
var re = new RegExp("ab+c");
. , , . , .
, /abc/, , /ab*c/ /Chapter (\d+)\.\d*/. , " ". , .
. , /abc/ 'abc' . "Hi, do you know your abc's?" "The latest airplane designs evolved from slabcraft." 'abc'. "Grab crab", 'abc'.
- , , 'b' , . , /ab*c/ , 'a' 'b' (* ), 'c'. "cbbabbbbcdebc," 'abbbbc'.
.
\
|
:
|
^
|
. , .
,
, |
$
|
. , .
, |
*
|
0 . {0,}.
, |
+
|
1 . {1,}.
, |
?
|
0 1 . {0,1}.
,
(assertions), x(?=y) x(?!y) . |
.
|
( ) .
, |
(x)
|
'x' . .
, |
(?:x)
|
'x' .
- .
[1], ..., [n].
|
x(?=y)
|
'x' 'x' 'y'. .
, |
x(?!y)
|
'x' 'x' 'y'. .
, |
x|y
|
'x' 'y'.
, |
{n}
|
n - .
, |
{n,m}
|
m n - .
, |
[xyz]
|
. .
, . (
(
, |
[^xyz]
|
. , . . , , .
, |
[\b]
|
(U+0008). ( \b.) |
\b
|
. ,
.
, .
, . (
: Note: JavaScript's regular expression engine defines a specific set of charactersto be "word" characters. Any character not in that set is considered a word break. This set of characters is fairly limited: it consists solely of the Roman alphabet in both upper- and lower-case, decimal digits, and the underscore character. Accented characters, such as "" or "" are, unfortunately, treated as word breaks. |
\B
|
. , : , . .
, |
\cX
|
X . .
, |
\d
|
.
, |
\D
|
.
, |
\f
|
(U+000C). . |
\n
|
(U+000A). |
\r
|
(U+000D). |
\s
|
, ,
, , .
, |
\S
|
.
, |
\t
|
(U+0009). |
\v
|
(U+000B). |
\w
|
.
, |
\W
|
.
, |
\n
|
n , , n , ( ).
, |
\0
|
NULL (U+0000).
, \0<digits>
.
|
\xhh
|
hh ( ) |
\uhhhh
|
hhhh ( ). |
, , :
function escapeRegExp(string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); // $& means the whole matched string
}
. , Using Parenthesized Substring Matches.
, /Chapter (\d+)\.\d*/ , . 'Chapter ', (\d , '+' 1 ), ( ; ' \' , '.'), 0 ('\d' , '*' 0 ). , .
"Open Chapter 4.3, paragraph 6" '4' . "Chapter 3 and 4", '3'.
, , ' ?:'. , (?:\d+) , .
test exec RegExp match, replace, search, split String. JavaScript
exec |
RegExp, . . |
test |
RegExp, . . |
match |
String, . null . |
search |
String, . , -1 . |
replace |
String, , , . |
split |
String, . |
, test search; exec match ( ). exec match , RegExp . , exec null ( false).
. , exec .
var myRe = /d(b+)d/g;
var myArray = myRe.exec("cdbbdbsbz");
, myArray :
var myArray = /d(b+)d/g.exec("cdbbdbsbz");
, :
var myRe = new RegExp("d(b+)d", "g");
var myArray = myRe.exec("cdbbdbsbz");
, . .
| . | |||
|---|---|---|---|
myArray |
. | ["dbbd", "bb"] |
|
index |
( ). | 1 |
|
input |
. | "cdbbdbsbz" |
|
[0] |
. | "dbbd" |
|
myRe |
lastIndex |
. . ( g, [Advanced Searching With Flags](#Advanced_Searching_With_Flags).) | 5 |
source |
. , . | "d(b+)d" |
, , , . , , . , :
var myRe = /d(b+)d/g;
var myArray = myRe.exec("cdbbdbsbz");
console.log("The value of lastIndex is " + myRe.lastIndex);
:
The value of lastIndex is 5
, :
var myArray = /d(b+)d/g.exec("cdbbdbsbz");
console.log("The value of lastIndex is " + /d(b+)d/g.lastIndex);
:
The value of lastIndex is 0
/d(b+)d/g , , lastIndex. , , .
"" . , /a(b)c/ 'abc' 'b'. Array elements [1], ..., [n].
. , . .
replace(), () . $1 $2 .
var re = /(\w+)\s(\w+)/;
var str = "John Smith";
var newstr = str.replace(re, "$2, $1");
console.log(newstr);
"Smith, John".
, . , .
| Flag | Description |
|---|---|
g |
. |
| i | . |
| m | . |
| y | , lastindex . |
:
var re = /pattern/flags;
var re = new RegExp("pattern", "flags");
, . .
, re = /\w+\s/g , , .
var re = /\w+\s/g;
var str = "fee fi fo fum";
var myArray = str.match(re);
console.log(myArray);
["fee ", "fi ", "fo "]. :
var re = /\w+\s/g;
:
var re = new RegExp("\\w+\\s", "g");
.
m , . m , ^ $ .
. .
. string.split() string.replace(). , ( ) , . , ( ) .
// The name string contains multiple spaces and tabs,
// and may have multiple spaces between first and last names.
var names = "Harry Trump ;Fred Barney; Helen Rigby ; Bill Abel ; Chris Hand ";
var output = ["---------- Original String\n", names + "\n"];
// Prepare two regular expression patterns and array storage.
// Split the string into array elements.
// pattern: possible white space then semicolon then possible white space
var pattern = /\s*;\s*/;
// Break the string into pieces separated by the pattern above and
// store the pieces in an array called nameList
var nameList = names.split(pattern);
// new pattern: one or more characters then spaces then characters.
// Use parentheses to "memorize" portions of the pattern.
// The memorized portions are referred to later.
pattern = /(\w+)\s+(\w+)/;
// New array for holding names being processed.
var bySurnameList = [];
// Display the name array and populate the new array
// with comma-separated names, last first.
//
// The replace method removes anything matching the pattern
// and replaces it with the memorized stringsecond memorized portion
// followed by comma space followed by first memorized portion.
//
// The variables $1 and $2 refer to the portions
// memorized while matching the pattern.
output.push("---------- After Split by Regular Expression");
var i, len;
for (i = 0, len = nameList.length; i < len; i++) {
output.push(nameList[i]);
bySurnameList[i] = nameList[i].replace(pattern, "$2, $1");
}
// Display the new array.
output.push("---------- Names Reversed");
for (i = 0, len = bySurnameList.length; i < len; i++) {
output.push(bySurnameList[i]);
}
// Sort by last name, then display the sorted array.
bySurnameList.sort();
output.push("---------- Sorted");
for (i = 0, len = bySurnameList.length; i < len; i++) {
output.push(bySurnameList[i]);
}
output.push("---------- End");
console.log(output.join("\n"));
. , . "Check", . ( ), . , , .
(?:, \d{3} | \(, \d{3}, \), ( )), , , , , ([-\/\.]), \d{3}, , \1, \d{4}.
0 \(?, \d{3}, 0 \)?, , , ([-\/\.]), . \d{3}, followed by the remembered match of a dash, forward slash, or decimal point \1, followed by four digits \d{4}.
"" , , "Enter".
<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<meta http-equiv="Content-Script-Type" content="text/javascript" />
<script type="text/javascript">
var re = /\(?\d{3}\)?([-\/\.])\d{3}\1\d{4}/;
function testInfo(phoneInput) {
var OK = re.exec(phoneInput.value);
if (!OK)
window.alert(RegExp.input + " isn't a phone number with area code!");
else window.alert("Thanks, your phone number is " + OK[0]);
}
</script>
</head>
<body>
<p>
Enter your phone number (with area code) and then click "Check". <br />The
expected format is like ###-###-####.
</p>
<form action="#">
<input id="phone" /><button
>
Check
</button>
</form>
</body>
</html>
This page was last modified on 14 . 2025 . 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 |