RegExp.prototype.hasIndices
Baseline
Widely available
This feature is well established and works across many devices and browser versions. Itâs been available across browsers since 2021å¹´9æ.
hasIndices 㯠RegExp ã¤ã³ã¹ã¿ã³ã¹ã®ããããã£ã§ããã®æ£è¦è¡¨ç¾ã§ d ãã©ã°ã使ç¨ããããã©ããã示ãã¾ãã
試ãã¦ã¿ã¾ããã
const regex1 = /foo/d;
console.log(regex1.hasIndices);
// äºæ³ãããçµæ: true
const regex2 = /bar/;
console.log(regex2.hasIndices);
// äºæ³ãããçµæ: false
解説
RegExp.prototype.hasIndices ã®å¤ã¯ d ãã©ã°ã使ç¨ããã¦ããå ´åã« true ã¨ãªããããã§ãªãå ´å㯠false ã¨ãªãã¾ããd ãã©ã°ã¯ãæ£è¦è¡¨ç¾ã®ç
§åçµæã«åãã£ããã£ã°ã«ã¼ãã®é¨åæååã®éå§ã¨çµäºã®ã¤ã³ããã¯ã¹ãå«ãããã¨ã示ãã¾ããããã¯æ£è¦è¡¨ç¾ã®è§£éãç
§åã®åä½ã夿´ãããã®ã§ã¯ãªããç
§åçµæã«è¿½å æ
å ±ãä¸ããã ãã§ãã
ãã®ãã©ã°ã¯ã主㫠exec() ã®è¿å¤ã«å½±é¿ãã¾ããd ãã©ã°ãåå¨ããå ´åãexec() ã«ãã£ã¦è¿ãããé
åã¯ãexec() ã¡ã½ããã®è¿å¤ã«è¨è¿°ããã¦ããããã«ã追å ã® indices ããããã£ãæã¡ã¾ããä»ã®ãã¹ã¦ã®æ£è¦è¡¨ç¾é¢é£ã®ã¡ã½ããï¼String.prototype.match() ãªã©ï¼ã¯ãå
é¨çã« exec() ãå¼ã³åºãã®ã§ãæ£è¦è¡¨ç¾ã« d ãã©ã°ãããå ´åãã¤ã³ããã¯ã¹ãè¿ãã¾ãã
hasIndices ã®è¨å®ã¢ã¯ã»ãµã¼ã¯ undefined ã§ãããã®ããããã£ãç´æ¥å¤æ´ãããã¨ã¯ã§ãã¾ããã
ä¾
ã°ã«ã¼ãã¨å¾æ¹åç § > ã°ã«ã¼ãã¨ä¸è´çµæã®æ·»åã®ä½¿ç¨ã«è©³ãã使ç¨ä¾ãããã¾ãã
hasIndices ã®ä½¿ç¨
const str1 = "foo bar foo";
const regex1 = /foo/dg;
console.log(regex1.hasIndices); // true
console.log(regex1.exec(str1).indices[0]); // [0, 3]
console.log(regex1.exec(str1).indices[0]); // [8, 11]
const str2 = "foo bar foo";
const regex2 = /foo/;
console.log(regex2.hasIndices); // false
console.log(regex2.exec(str2).indices); // undefined
仿§æ¸
| Specification |
|---|
| ECMAScript® 2027 Language Specification> # sec-get-regexp.prototype.hasIndices> |