[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Global_Objects/RegExp [Back]  [Original]

RegExp - 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

RegExp

Baseline Widely available *

This feature is well established and works across many devices and browser versions. Its been available across browsers since 2015 ..

* Some parts of this feature may have varying levels of support.

RegExp .

, , JavaScript.

In this article

const regex1 = /\w+/;
const regex2 = new RegExp("\\w+");

console.log(regex1);
// Expected output: /\w+/

console.log(regex2);
// Expected output: /\w+/

console.log(regex1 === regex2);
// Expected output: false

, :

/pattern/flags
new RegExp(pattern, flags)

pattern

.

flags

, :

g

i

m

; (^ $) ( , ( \n \r), )

y

; , lastIndex ( ).

RegExp: . , - . :

js
/ab+c/i;
new RegExp("ab+c", "i");

. . , , , .

, , new RegExp('ab+c'), . -, , , , .

- ( \). , :

js
var re = /\w+/;
var re = new RegExp("\\w+");

.

(, ) : \n, \r, \u2028 \u2029.

, m . [^] (, IE), , .

, /./ , , , .

\d

. [0-9].

, /\d/ /[0-9]/ 2 B2 .

\D

, . [^0-9].

, /\D/ /[^0-9]/ B B2 .

\w

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

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

\W

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

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

\s

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

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

\S

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

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

\t .
\r .
\n .
\v .
\f .
[\b] ( \b).
\0 . .
\cX

X A Z. .

, /\cM/ control-M .

\xhh hh ( ).
\uhhhh hhhh ( ).
\

, , , .

, /b/ b. , /\b/, , .

, , , .

, * , ; /a*/ a. * ; , /a\*/ a*.

[xyz]

. . .

, [] , [-]. .

[^xyz]

. , . .

, [^] , [^-]. .

^

c . , .

, /^/ , .

$

c . , .

, /$/ , .

\b

, ( [\b]).

, /\b/ ; /\b/ .

\B

- , .

, /\B/ ; /\B/ .

(x)

x . .

, /(foo)/ foo foo bar. [1], ..., [n] $1, ..., $9 RegExp.

. , ( ).

\n

n . n- ( ).

, /(,)\s\1/ , , , , , . .

(?:x) x, . . [1], ..., [n] $1, ..., $9 RegExp.
x*

x .

, /*/ , .

x+

x . {1,}.

, /+/ .

x*?
x+?

x * +, , .

, /".*?"/ "foo" "foo" "bar" "foo" "bar", * ?.

x?

x .

, /??/ .

- *, +, ?, {}, ( ), ( ).

(?=), (?!) (?:), .

x(?=y) x, x y. , /(?=)/ . /(?=|)/ . , , .
x(?!y)

x, x y. , /\d+(?!\.)/ .

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

(?<=y)x

x, x y

, /(?<=)/ "" "".
/(?<=|)/ "" "" "".
, "" "" .

(?<!y)x

x, x y

, /(?<!-)\d+/ , .
/(?<!-)\d+/.exec('3') "3".
/(?<!-)\d+/.exec('-3') , 3 .

x|y

x, y.

, /|/ .

x{n}

n . n x.

, /{2}/ , .

x{n,}

n . n x.

, /{2,}/ , .

x{n,m}

n m . n , m x.

, /{1,3}/ , , . , , .

RegExp

.

RegExp.length

RegExp.length 2.

RegExp , , .

:

replace() String , . $1 $2, .

js
var re = /(\w+)\s(\w+)/;
var str = "John Smith";
var newstr = str.replace(re, "$2, $1");
console.log(newstr);

//    
var re = /([-]+)\s([-]+)/i;
var str = " ";
var newstr = str.replace(re, "$2, $1");
console.log(newstr);

Smith, John ,

:

(Unix, Windows ). .

js
var text = " \n \r\n \r ";
var lines = text.split(/\r\n|\r|\n/);
console.log(lines); //  [ ' ', ' ', ' ', ' ' ]

, .

:

js
var s = "Please yes\nmake my day!";
s.match(/yes.*day/);
//  null
s.match(/yes[^]*day/);
//  'yes\nmake my day'

:

, .

js
var text = " \n ";
var regex = /(\S+) \n?/y;

var match = regex.exec(text);
console.log(match[1]); //  ''
console.log(regex.lastIndex); //  '14'

var match2 = regex.exec(text);
console.log(match2[1]); //  ''
console.log(regex.lastIndex); //  '27'

var match3 = regex.exec(text);
console.log(match3 === null); //  'true'

, , try { } catch { }. eval(), RegExp(-, --) ( // , , catch). :

js
var supports_sticky;
try {
  RegExp("", "y");
  supports_sticky = true;
} catch (e) {
  supports_sticky = false;
}
console.log(supports_sticky); //  'true'

:

, \w \W ASCII; , a z, A Z, 0 9 _. , , , \uhhhh, hhhh , . , , .

js
var text = " text   ";
var regex = /[\u0400-\u04FF]+/g;

var match = regex.exec(text);
console.log(match[0]); //  ''
console.log(regex.lastIndex); //  '7'

var match2 = regex.exec(text);
console.log(match2[0]); //  '' [ 'text']
console.log(regex.lastIndex); //  '15'

//   

: regexp-unicode-block.

: URL

js
var url = "http://xxx.domain.com";
console.log(/[^.]+/.exec(url)[0].substr(7)); //  'xxx'

Specification
ECMAScript 2027 LanguageSpecification
# sec-regexp-regular-expression-objects

Gecko

Gecko 34, , , undefined :

js
// Firefox 33   
"x".replace(/x(.)?/g, function (m, group) {
  console.log("'group:" + group + "'");
}); // 'group:'

// Firefox 34   
"x".replace(/x(.)?/g, function (m, group) {
  console.log("'group:" + group + "'");
}); // 'group:undefined'

, , RegExp.$N - undefined (bug 1053944).


Web Proxy Viewer  |  New URL  |  Original Page