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

RegExp - JavaScript | MDN

MDN Web Docs

View in English Always switch to English

RegExp

Baseline *

20157

*

RegExp

JavaScript

RegExp 2

  • 2 2
  • RegExp 2

3

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

RegExp new RegExp('ab+c')

RegExp()

new RegExp(/ab+c/, flags) RegExp 1 2

\

2

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

: RegExp

x

  1. x
  2. x[Symbol.match] undefined
  3. x[Symbol.match] undefined x RegExp x RegExp Symbol.match

Symbol.match

[Symbol.match]() exec

String.prototype.endsWith()

js
"foobar".endsWith({ toString: () => "bar" }); // true
"foobar".endsWith(/bar/); // TypeError: First argument to String.prototype.endsWith must not be a regular expression

[Symbol.match] undefined String.prototype.match() [Symbol.match] match() re.toString() 2 RegExp

js
const re = /bar/g;
re[Symbol.match] = false;
"/bar/g".endsWith(re); // true
re.exec("bar"); // [ 'bar', index: 0, input: 'bar', groups: undefined ]
"bar & bar".replace(re, "foo"); // 'foo & foo'

Perl RegExp

RegExp Perl Perl JavaScript RegExp

RegExp()

RegExp

RegExp.$1, , RegExp.$9

RegExp.input ($_)

RegExp.lastMatch ($&)

RegExp.lastParen ($+)

RegExp.leftContext ($`)

RegExp.rightContext ($')

RegExp[Symbol.species]

RegExp.escape()

RegExp()

RegExp.prototype RegExp

RegExp.prototype.constructor

RegExp RegExp

RegExp.prototype.dotAll

.

RegExp.prototype.flags

RegExp

RegExp.prototype.global

RegExp.prototype.hasIndices

RegExp.prototype.ignoreCase

RegExp.prototype.multiline

RegExp.prototype.source

RegExp.prototype.sticky

(sticky)

RegExp.prototype.unicode

Unicode

RegExp.prototype.unicodeSets

u v

RegExp

lastIndex

RegExp.prototype.compile()

()

RegExp.prototype.exec()

RegExp.prototype.test()

RegExp.prototype.toString()

Object.prototype.toString()

RegExp.prototype[Symbol.match]()

RegExp.prototype[Symbol.matchAll]()

RegExp.prototype[Symbol.replace]()

RegExp.prototype[Symbol.search]()

RegExp.prototype[Symbol.split]()

String.prototype.replace() ,

$1 $2

js
const re = /(\w+)\s(\w+)/;
const str = "Maria Cruz";
const newStr = str.replace(re, "$2, $1");
console.log(newStr);

"Cruz, Maria"

//

(UnixWindows )

js
const text = "Some text\nAnd some more\r\nAnd yet\nThis is the end";
const lines = text.split(/\r?\n/);
console.log(lines); // [ 'Some text', 'And some more', 'And yet', 'This is the end' ]

. s dotAll

js
const s = "Please yes\nmake my day!";

s.match(/yes.*day/);
// null 

s.match(/yes.*day/s);
// Returns ["yes\nmake my day"]

sticky

sticky sticky RegExp.prototype.lastIndex

js
const str = "#foo#";
const regex = /foo/y;

regex.lastIndex = 1;
regex.test(str); // true
regex.lastIndex = 5;
regex.test(str); // false (sticky  lastIndex )
regex.lastIndex; // 0 ()

sticky global

sticky y lastIndex global g lastIndex

js
const re = /\d/y;
let r;
while ((r = re.exec("123 456"))) {
  console.log(r, "AND re.lastIndex", re.lastIndex);
}

// [ '1', index: 0, input: '123 456', groups: undefined ] AND re.lastIndex 1
// [ '2', index: 1, input: '123 456', groups: undefined ] AND re.lastIndex 2
// [ '3', index: 2, input: '123 456', groups: undefined ] AND re.lastIndex 3
//   ... 

global g 3 6

Unicode

\w \W ASCII a z A Z 0 9 _

ASCII \uHHHH HHHH 16 Unicode

Unicode

js
const text = " text   ";
const regex = /[\u0400-\u04ff]+/g;

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

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

// 

Unicode \p{scx=Cyrl} \p{L}/u

URL

js
const url = "http://xxx.domain.com";
console.log(/^https?:\/\/(.+?)\./.exec(url)[1]); // 'xxx'

: URL URL API URL

js
const breakfasts = ["bacon", "eggs", "oatmeal", "toast", "cereal"];
const order = "Let me get some bacon and eggs, please";

order.match(new RegExp(`\\b(${breakfasts.join("|")})\\b`, "g"));
// ['bacon', 'eggs'] 

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

Firefox

Firefox 34 undefined

js
// Firefox 33 
"x".replace(/x(.)?/g, (m, group) => {
  console.log(`group: ${JSON.stringify(group)}`);
});
// group: ""

// Firefox 34 
"x".replace(/x(.)?/g, (m, group) => {
  console.log(`group: ${group}`);
});
// group: undefined

RegExp.$N undefined ( 1053944)


Web Proxy Viewer  |  New URL  |  Original Page