| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/RegExp | [Back] [Original] |
Get to know MDN better
RegExp 2
3
const re = /ab+c/i; //
//
const re = new RegExp("ab+c", "i"); //
//
const re = new RegExp(/ab+c/, "i"); //
RegExp new RegExp('ab+c')
new RegExp(/ab+c/, flags) RegExp 1 2
\
2
const re = /\w+/;
//
const re = new RegExp("\\w+");
x x[Symbol.match] undefined x[Symbol.match] undefined x RegExp x RegExp Symbol.match Symbol.match
RegExp Symbol.match undefined exec [Symbol.replace]() RegExp Symbol.match [Symbol.match]() exec
String.prototype.endsWith(), startsWith(), includes() TypeError String.prototype.matchAll() replaceAll() global [Symbol.matchAll]() [Symbol.replace]() RegExp() pattern pattern pattern pattern source flags "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
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'
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.prototype RegExp
RegExp.prototype.constructor RegExp RegExp
RegExp.prototype.dotAll.
RegExp.prototype.flagsRegExp
RegExp.prototype.globalRegExp.prototype.hasIndicesRegExp.prototype.ignoreCaseRegExp.prototype.multilineRegExp.prototype.sourceRegExp.prototype.sticky(sticky)
RegExp.prototype.unicodeUnicode
RegExp.prototype.unicodeSetsu v
RegExp
RegExp.prototype.compile() ()
RegExp.prototype.exec()RegExp.prototype.test()RegExp.prototype.toString()RegExp.prototype[Symbol.match]()RegExp.prototype[Symbol.matchAll]()RegExp.prototype[Symbol.replace]()RegExp.prototype[Symbol.search]()RegExp.prototype[Symbol.split]() $1 $2
const re = /(\w+)\s(\w+)/;
const str = "Maria Cruz";
const newStr = str.replace(re, "$2, $1");
console.log(newStr);
"Cruz, Maria"
(UnixWindows )
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
const s = "Please yes\nmake my day!";
s.match(/yes.*day/);
// null
s.match(/yes.*day/s);
// Returns ["yes\nmake my day"]
sticky sticky RegExp.prototype.lastIndex
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 y lastIndex global g lastIndex
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
\w \W ASCII a z A Z 0 9 _
ASCII \uHHHH HHHH 16 Unicode
Unicode
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
const url = "http://xxx.domain.com";
console.log(/^https?:\/\/(.+?)\./.exec(url)[1]); // 'xxx'
: URL URL API URL
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 34 undefined
// 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 |