[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ru/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 . exec test RegExp match, replace, search, split String. JavaScript.

In this article

:

  • , :

    js
    var re = /ab+c/;
    

    . , , .

  • RegExp, :

    js
    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'.

.

4.1 .
\ :
  • , .
  • , /b/ 'b'. b, . /\b/, , .
  • , .
  • , * , 0 ; , /a*/ 0 . *, ; , /a\*/ 'a*'.
  • \ new RegExp("pattern") \ .
^

. , .

, /^A/ 'A' "an A", 'A' "An E".


.

, /[^a-z\s]/ 'I' "I have 3 sisters".

$

. , .

, /t$/ 't' "eater", "eat".

*

0 . {0,}.

, /bo*/ 'boooo' "A ghost booooed" 'b' "A bird warbled", "A goat grunted".

+

1 . {1,}.

, /a+/ 'a' "candy" 'a' "caaaaaaandy".

?

0 1 . {0,1}.

, /e?le?/ 'el' "angel" 'le' "angle" 'l' "oslo".

*, +, ?, {}, "" ( ), , "" ( ). , /\d+/ "123abc" "123", /\d+?/, "1" .

(assertions), x(?=y) x(?!y) .

.

( ) .

, /.n/ 'an' 'on' "nay, an apple is on the tree", 'nay'.

(x)

'x' . .

, /(foo)/ 'foo' "foo bar." [1], ..., [n].

(?:x) 'x' . - . [1], ..., [n].
x(?=y)

'x' 'x' 'y'. .

, /Jack(?=Sprat)/ 'Jack' 'Sprat'. /Jack(?=Sprat|Frost)/ 'Jack' 'Sprat' 'Frost'. , 'Sprat' 'Frost' .

x(?!y)

'x' 'x' 'y'. .

, /\d+(?!\.)/ . /\d+(?!\.)/.exec("3.141") '141' '3.141'.

x|y

'x' 'y'.

, /green|red/ 'green' "green apple" 'red' "red apple."

{n}

n - . n .

, /a{2}/ 'a' "candy," "caandy," "caaandy."

{n,m}

m n - . n m . m=n=1 .

, /a{1,3}/ "cndy", 'a' "candy," "caandy," "caaaaaaandy". , "caaaaaaandy", "aaa", .

[xyz]

. . , . ( (.) (*)) . . .

, [abcd] [a-d]. 'b' "brisket" 'c' "city". /[a-z.]+/ /[\w.]+/ "test.i.ng".

[^xyz]

. , . . , , .

, [^abc] [^a-c]. 'r' "brisket" 'h' "chop."

[\b] (U+0008). ( \b.)
\b

. , . , . , . ( [\b].)

:
/\bmoo/ 'moo' "moon" ;
/oo\b/ 'oo' "moon", 'oo' 'n' , ;
/oon\b/ 'oon' "moon", 'oon' , , ;
/\w\b\w/ , , .

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

. , : , . .

, /\B../ 'oo' "noonday" (, /y\B./ 'ye' "possibly yesterday."

\cX

X . .

, /\cM/ control-M (U+000D) .

\d

. [0-9].

, /\d/ or /[0-9]/ '2' "B2 is the suite number."

\D

. [^0-9].

, /\D/ or /[^0-9]/ 'B' "B2 is the suite number."

\f

(U+000C). .

\n (U+000A).
\r (U+000D).
\s

, , , , . [ \f\n\r\t\v\u00A0\u1680\u180e\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u2028\u2029\u2028\u2029\u202f\u205f\u3000].

, /\s\w*/ ' bar' "foo bar."

\S

. [^ \f\n\r\t\v\u00A0\u1680\u180e\u2000-\u200a\u2028\u2029\u202f\u205f\u3000].

, /\S\w*/ 'foo' "foo bar."

\t (U+0009).
\v (U+000B).
\w

. [A-Za-z0-9_].

, /\w/ 'a' "apple," '5' "$5.28," '3' "3D."

\W

. [^A-Za-z0-9_].

, /\W/ or /[^A-Za-z0-9_]/ '%' "50%."

\n

n , , n , ( ).

, /apple(,)\sorange\1/ 'apple, orange,' "apple, orange, cherry, peach."

\0 NULL (U+0000). , \0<digits> .
\xhh hh ( )
\uhhhh hhhh ( ).

, , :

js
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 .

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

, myArray :

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

, :

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

, . .

Table 4.3
.
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"

, , , . , , . , :

js
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

, :

js
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 .

js
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 .

:

js
var re = /pattern/flags;

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

, . .

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

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

["fee ", "fi ", "fo "]. :

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

:

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

.

m , . m , ^ $ .

. .

. string.split() string.replace(). , ( ) , . , ( ) .

js
// 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".

html
<!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>

Web Proxy Viewer  |  New URL  |  Original Page