「JavaScript」RegExpクラスのSymbol.matchメソッドの使い方

構文
regexp[Symbol.match](str)
引数
str 一致の対象となる文字列
戻り値
一致した全体の結果と括弧が捕捉した一致箇所の結果を含む Array。一致するものがなかった場合は、null。
[@@match]() メソッドは、文字列の正規表現に一致した部分を取得します。
書式

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
class クラス名 extends RegExp{
[Symbol.match](引数) {
var 変数名 = RegExp.prototype[Symbol.match].call(this,引数);
}
}
class クラス名 extends RegExp{ [Symbol.match](引数) { var 変数名 = RegExp.prototype[Symbol.match].call(this,引数); } }
class クラス名 extends RegExp{
 [Symbol.match](引数) {
   var 変数名 = RegExp.prototype[Symbol.match].call(this,引数);
 }
}

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
class RegExpA extends RegExp {
[Symbol.match](str) {
const result = RegExp.prototype[Symbol.match].call(this, str);
if (result) {
return '文字列の正規表現に一致';
}
return '文字列の正規表現に一致しません';
}
}
var regex = new RegExpA('([0-9]+)([A-Z]+)-([a-z]+)');
console.log('2012AUX-ark'.match(regex));
console.log('AUX-ark'.match(regex));
class RegExpA extends RegExp { [Symbol.match](str) { const result = RegExp.prototype[Symbol.match].call(this, str); if (result) { return '文字列の正規表現に一致'; } return '文字列の正規表現に一致しません'; } } var regex = new RegExpA('([0-9]+)([A-Z]+)-([a-z]+)'); console.log('2012AUX-ark'.match(regex)); console.log('AUX-ark'.match(regex));
class RegExpA extends RegExp {
  [Symbol.match](str) {
    const result = RegExp.prototype[Symbol.match].call(this, str);
    if (result) {
      return '文字列の正規表現に一致';
    }
    return '文字列の正規表現に一致しません';
  }
}

var regex = new RegExpA('([0-9]+)([A-Z]+)-([a-z]+)');
console.log('2012AUX-ark'.match(regex));
console.log('AUX-ark'.match(regex));

実行結果
> “文字列の正規表現に一致AA"
> “文字列の正規表現に一致しませんBB"

JavaScript

Posted by arkgame