[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ja/docs/Web/JavaScript/Guide/Regular_expressions [Back]  [Original]

- JavaScript | MDN

MDN Web Docs

View in English Always switch to English

JavaScript RegExp exec() test() String match()matchAll()replace()replaceAll()search()split() JavaScript

2

  • js
    const re = /ab+c/;
    

  • RegExp

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

: /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"

JavaScript

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()

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

myArray

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

exec()

js
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

js
const myRe = /d(b+)d/g;
const myArray = myRe.exec("cdbbdbsbz");
console.log(`lastIndex  ${myRe.lastIndex} `);

// "lastIndex  5 "

js
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

js
const re = /pattern/flags;

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

re = /\w+\s/g 1

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

// ["fee ", "fi ", "fo "]

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

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

m m ^ $

i, m, s

exec()

RegExp.prototype.exec() g

js
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

String.prototype.match()

js
console.log(str.match(re)); // ["fee ", "fi ", "fo "]

Unicode

u "unicode" Unicode Unicode Unicode Unicode

js
/\p{L}*/u;

Unicode RegExp.prototype.unicode

[] ()

  1. : ^
  2. 3 \d{3} | \( 3 \d{3} \) (?:)
  3. 1 ()
  4. 3 \d{3}
  5. () \1
  6. 4 \d{4}
  7. : $

HTML

html
<p>
   "" 
  <br />
   ###-###-#### 
</p>
<form id="form">
  <input id="phone" />
  <button type="submit"></button>
</form>
<p id="output"></p>

JavaScript

js
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);
});

RegExr

Regex tester

/

Regex interactive tutorial

Regex visualizer


Web Proxy Viewer  |  New URL  |  Original Page