Baseline Widely available
match()
㯠String
å¤ã®ã¡ã½ããã§ããã®æååã®æ£è¦è¡¨ç¾ã«å¯¾ããç
§åçµæãåãåãã¾ãã
const paragraph = "The quick brown fox jumps over the lazy dog. It barked.";
const regex = /[A-Z]/g;
const found = paragraph.match(regex);
console.log(found);
// Expected output: Array ["T", "I"]
æ§æ 弿°
regexp
æ£è¦è¡¨ç¾ãªãã¸ã§ã¯ããã¾ã㯠Symbol.match
ãæã¤ä»»æã®ãªãã¸ã§ã¯ãã§ãã
regexp
ã RegExp
以å¤ã®ãªãã¸ã§ã¯ãã§ãã£ãå ´åãæé»çã« RegExp
ã¸ã®å¤æã new RegExp(regexp)
ã使ç¨ãã¦è¡ããã¾ãã
ä¸å弿°ãä¸ããã« match()
ã¡ã½ããã使ã£ãå ´åã空æåå 1 ã¤ãæã¤ Array
ã[""]
ãå¾ããã¾ãããã㯠match(/(?:)/)
ã¨åãã«ãªãããã§ãã
Array
ãè¿ãã¾ããããã¯ã°ãã¼ãã« (g
) ãã©ã°ã®æç¡ã«ãã£ã¦å
容ãå¤ããã¾ããä¸è´ãããã®ãè¦ã¤ãããªãã£ãå ´å㯠null
ãè¿ãã¾ãã
g
ãã©ã°ããã£ãå ´åã¯ãæ£è¦è¡¨ç¾å
¨ä½ã«ä¸è´ãããã¹ã¦ã®çµæãè¿ãã¾ããããã£ããã£ã°ã«ã¼ãã¯è¿ãã¾ãããg
ãã©ã°ããªãã£ãå ´åãæåã«å®å
¨ã«ä¸è´ãããã®ã¨ãããã«é¢ãããã£ããã£ã°ã«ã¼ããè¿ãã¾ãããã®å ´åãmatch()
㯠RegExp.prototype.exec()
ã¨åãçµæã«ãªãã¾ãï¼è¿½å ã®ããããã£ä»ãã®é
åï¼ãString.prototype.match
èªä½ã®å®è£
ã¯ã¨ã¦ãã·ã³ãã«ã§ãã弿°ã®æååãæåã®å¼æ°ã¨ã㦠Symbol.match
ã¡ã½ãããå¼ã³åºãã ãã§ããå®éã®å®è£
㯠RegExp.prototype[Symbol.match]()
ããæ¥ã¦ãã¾ãã
RegExp
ã«ä¸è´ãããã©ãããç¥ãå¿
è¦ãããå ´åã¯ã RegExp.prototype.test()
ã使ç¨ãã¦ãã ãããRegExp.prototype.exec()
ã使ã£ãã»ããè¯ãããããã¾ãããRegExp.prototype.exec()
ã使ç¨ãã¦ãã ãããæ£è¦è¡¨ç¾ã渡ãããã¨ãã® match()
ã®æå³ã«ã¤ãã¦ã®è©³ããæ
å ±ã¯ãRegExp.prototype[Symbol.match]()
ãåç
§ãã¦ãã ããã
以ä¸ã®ä¾ã«ããã¦ã match()
㯠"Chapter"
ã¨ããã«ç¶ã 1 æ¡ä»¥ä¸ã®æ°åãããã«ç¶ã 0 å以ä¸ã®å°æ°ç¹ã¨æ°åãè¦ã¤ããããã«ä½¿ããã¦ãã¾ãã
æ£è¦è¡¨ç¾ã i
ãã©ã°ãå«ãã§ããã®ã§ã大æåã¨å°æåã®éãã¯ç¡è¦ããã¾ãã
const str = "For more information, see Chapter 3.4.5.1";
const re = /see (chapter \d+(\.\d)*)/i;
const found = str.match(re);
console.log(found);
// [
// 'see Chapter 3.4.5.1',
// 'Chapter 3.4.5.1',
// '.1',
// index: 22,
// input: 'For more information, see Chapter 3.4.5.1',
// groups: undefined
// ]
ä¸ã®ç
§åçµæã§ã¯ã'see Chapter 3.4.5.1'
ãä¸è´ããæååå
¨ä½ã§ãã 'Chapter 3.4.5.1'
㯠(chapter \d+(\.\d)*)
ã«ãã£ã¦ææããã¾ããã '.1'
㯠(\.\d)
ã«ãã£ã¦ææãããæå¾ã®å¤ã§ãã index
ãããã㣠(22
) ã¯ä¸è´ããæååå
¨ä½ã® 0 åºç¹ã®ã¤ã³ããã¯ã¹ã§ãã input
ããããã£ã¯è§£éã§ããå
ã®æååã§ãã
以ä¸ã®ä¾ã¯ã g 㨠i ãã©ã°ã match()
ã§ä½¿ç¨ããå®ä¾ã§ãã A
ãã E
ã¾ã§ã¨ã a
ãã e
ã¾ã§ã®ãã¹ã¦ã®æåãè¿ããããããããé
åã®åã
ã®è¦ç´ ã«å
¥ãã¾ãã
const str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
const regexp = /[A-E]/gi;
const matches = str.match(regexp);
console.log(matches);
// ['A', 'B', 'C', 'D', 'E', 'a', 'b', 'c', 'd', 'e']
ã¡ã¢: String.prototype.matchAll()
ã¨ãã©ã°ãç¨ããé«åº¦ãªæ¤ç´¢ãåç
§ãã¦ãã ããã
ååä»ããã£ããã£ã°ã«ã¼ãã«å¯¾å¿ãã¦ãããã©ã¦ã¶ã¼ã§ã¯ã次ã®ã³ã¼ã㯠"fox"
ã¾ã㯠"cat"
ã animal
ã¨ããååã®ã°ã«ã¼ãã«å
¥ãã¾ãã
const paragraph = "The quick brown fox jumps over the lazy dog. It barked.";
const capturingRegex = /(?<animal>fox|cat) jumps over/;
const found = paragraph.match(capturingRegex);
console.log(found.groups); // {animal: "fox"}
弿°ãªãã® match() ã®ä½¿ç¨
const str = "Nothing will come of nothing.";
str.match(); // returns [""]
match() ã [Symbol.match]()
ãå®è£
ãã¦ãã RegExp ã§ãªããªãã¸ã§ã¯ãã§ä½¿ç¨
ãªãã¸ã§ã¯ãã« Symbol.match
ã¡ã½ãããããå ´åãã«ã¹ã¿ã ãããã£ã¼ã¨ãã¦ä½¿ç¨ãããã¨ãã§ãã¾ãã Symbol.match
ã®è¿å¤ã match()
ã®è¿å¤ã«ãªãã¾ãã
const str = "Hmm, this is interesting.";
str.match({
[Symbol.match](str) {
return ["Yes, it's interesting."];
},
}); // returns ["Yes, it's interesting."]
RegExp 以å¤ã弿°ã¨ãã¦åã
æ£è¦è¡¨ç¾ regexp
弿°ãæååã¾ãã¯æ°å¤ã®å ´åãRegExp
ã« new RegExp(regexp)
ã使ç¨ãã¦æé»çã«å¤æããã¾ãã
const str1 =
"NaN means not a number. Infinity contains -Infinity and +Infinity in JavaScript.";
const str2 =
"My grandfather is 65 years old and My grandmother is 63 years old.";
const str3 = "The contract was declared null and void.";
str1.match("number"); // "number" ã¯æååã§ãã ["number"] ãè¿ãã¾ãã
str1.match(NaN); // NaN ã®åã¯æ°å¤ã§ãã ["NaN"] ãè¿ãã¾ãã
str1.match(Infinity); // Infinity ã®æ¹ã¯æ°å¤ã§ãã ["Infinity"] ãè¿ãã¾ãã
str1.match(+Infinity); // ["Infinity"] ãè¿ãã¾ã
str1.match(-Infinity); // ["-Infinity"] ãè¿ãã¾ã
str2.match(65); // ["65"] ãè¿ãã¾ã
str2.match(+65); // æ£ã®ç¬¦å·ãä»ããæ°å¤ã§ãã ["65"] ãè¿ãã¾ã
str3.match(null); // ["null"] ãè¿ãã¾ãã
ç¹æ®æåãé©åã«ã¨ã¹ã±ã¼ãããã¦ããªãã¨ãäºæããªãçµæã«ãªããã¨ãããã¾ãã
console.log("123".match("1.3")); // [ "123" ]
æ£è¦è¡¨ç¾ä¸ã® .
ã¯ã©ã®ãããªæåã«ãä¸è´ããã®ã§ãããã¯ä¸è´ãã¾ãã ãããæåã«ã®ã¿ä¸è´ããããã«ããã«ã¯ãå
¥åæååãã¨ã¹ã±ã¼ãããå¿
è¦ãããã¾ãã
console.log("123".match("1\\.3")); // null
仿§æ¸ ãã©ã¦ã¶ã¼ã®äºææ§ é¢é£æ
å ±
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4