| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/ja/docs/Web/JavaScript/Guide/Regular_expressions | [Back] [Original] |
Get to know MDN better
JavaScript RegExp exec() test() String match()matchAll()replace()replaceAll()search()split()
JavaScript
2
const re = /ab+c/;
const 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" "ab c" "abc"
1 b
1 "a" 0 "b" "c" /ab*c/ "b" * 0
"cbbabbbbcdebc" "abbbbc"
| / | |
|---|---|
[xyz], [^xyz], .,
\d, \D, \w, \W,
\s, \S, \t, \r,
\n, \v, \f, [\b],
\0, \cX, \xhh,
\uhhhh, \u{hhhh},
x|y
|
|
^, $, \b, \B,
x(?=y), x(?!y), (?<=y)x,
(?<!y)x
|
|
(x), (?<Name>x), (?:x),
\n, \k<Name>
|
|
x*, x+, x?,
x{n}, x{n,},
x{n,m}
|
( "*" ) "a" "*" "b" /a\*b/ "*"
("/")
"/example/" 1 /\/example\/[a-z]+/i
"C:\" "C" /[A-Z]:\\/
RegExp
/a\*b/ new RegExp("a\\*b") "a" "*" "b"
RegExp.escape() new RegExp(RegExp.escape("a*b")) "a*b"
RegExp test() exec() String match()matchAll()replace()replaceAll(), search()split()
exec() |
null |
test() |
true false |
match() |
null |
matchAll() |
|
search() |
-1 |
replace() |
|
replaceAll() |
|
split() |
test() search() exec() match()
exec() match() RegExp
exec null false
exec()
const myRe = /d(b+)d/g;
const myArray = myRe.exec("cdbbdbsbz");
myArray
const myArray = /d(b+)d/g.exec("cdbbdbsbz");
// 'cdbbdbsbz'.match(/d(b+)d/g)
// 'cdbbdbsbz'.match(/d(b+)d/g) [ "dbbd" ]
// /d(b+)d/g.exec('cdbbdbsbz') [ 'dbbd', 'bb', index: 1, input: 'cdbbdbsbz' ]
const myRe = new RegExp("d(b+)d", "g");
const myArray = myRe.exec("cdbbdbsbz");
myArray |
['dbbd', 'bb', index: 1, input: 'cdbbdbsbz'] |
||
index |
0 | 1 |
|
input |
'cdbbdbsbz' |
||
[0] |
'dbbd' |
||
myRe |
lastIndex |
g | 5 |
source |
'd(b+)d' |
2
const myRe = /d(b+)d/g;
const myArray = myRe.exec("cdbbdbsbz");
console.log(`lastIndex ${myRe.lastIndex} `);
// "lastIndex 5 "
const myArray = /d(b+)d/g.exec("cdbbdbsbz");
console.log(`lastIndex ${/d(b+)d/g.lastIndex} `);
// "lastIndex 0 "
2 /d(b+)d/g lastIndex
6
d |
hasIndices |
|
g |
global |
|
i |
ignoreCase |
|
m |
^ $ |
multiline |
s |
. |
dotAll |
u |
"unicode" Unicode | unicode |
v |
Unicode u |
unicodeSets |
y |
(sticky) | sticky |
const re = /pattern/flags;
const re = new RegExp("pattern", "flags");
re = /\w+\s/g 1
const re = /\w+\s/g;
const str = "fee fi fo fum";
const myArray = str.match(re);
console.log(myArray);
// ["fee ", "fi ", "fo "]
const re = /\w+\s/g;
const re = new RegExp("\\w+\\s", "g");
m
m ^ $
const str = "fee fi fo fum";
const re = /\w+\s/g;
console.log(re.exec(str)); // ["fee ", index: 0, input: "fee fi fo fum"]
console.log(re.exec(str)); // ["fi ", index: 4, input: "fee fi fo fum"]
console.log(re.exec(str)); // ["fo ", index: 7, input: "fee fi fo fum"]
console.log(re.exec(str)); // null
console.log(str.match(re)); // ["fee ", "fi ", "fo "]
u "unicode" Unicode Unicode Unicode Unicode
/\p{L}*/u;
Unicode RegExp.prototype.unicode
[] ()
^\d{3} | \( 3 \d{3} \) (?:) () \d{3}\1\d{4}$<p>
""
<br />
###-###-####
</p>
<form id="form">
<input id="phone" />
<button type="submit"></button>
</form>
<p id="output"></p>
const form = document.querySelector("#form");
const input = document.querySelector("#phone");
const output = document.querySelector("#output");
const re = /^(?:\d{3}|\(\d{3}\))([-/.])\d{3}\1\d{4}$/;
function testInfo(phoneInput) {
const ok = re.exec(phoneInput.value);
output.textContent = ok
? ` ${ok[0]}`
: `${phoneInput.value} `;
}
form.addEventListener("submit", (event) => {
event.preventDefault();
testInfo(input);
});
| Web Proxy Viewer | New URL | Original Page |