Home » ★JavaScriptリファレンス

★JavaScriptリファレンス

RegExp.$1-$9 …… 正規表現に含まれる括弧内のパターンにマッチした部分を返す

Internet Explorer
Google Chrome
Safari
Firefox
Opera
広告



RegExpオブジェクトの$1-$9は、正規表現に含まれる括弧内のパターンにマッチした部分を返します。

RegExpオブジェクトにおける$1、$2、$3、$4、$5、$6、$7、$8、$9は、特別な変数となります。
1~9の数値は、1番目の括弧・2番目の括弧・3番目の括弧…という具合に括弧が現れる順番に対応しています。

例えば、/(.*)(ball)(s)(.*)/ という正規表現に対して「Baseball News」という文字列をマッチさせる場合、
1番目の括弧 (.*) は「任意の文字列」というパターンなので「Base」の部分がマッチします。
2番目の括弧 (ball) は「”ball”という文字列」というパターンなので「ball」の部分がマッチします。
3番目の括弧 (s) は「半角スペースなど」というパターンなので「Baseball」と「News」の間の半角スペースの部分がマッチします
4番目の括弧 (.*) は「任意の文字列」というパターンなので「News」の部分がマッチします。
そして、特別な変数である$1、$2、$3、$4には、それぞれ「Base」「ball」「 」「News」が代入されます。

仮に、/(.*)(ball)(s)(.*)/ という正規表現はそのままに、対象文字列を「Volleyball Player」に変更した場合、
$1、$2、$3、$4には、それぞれ「Volley」「ball」「 」「Player」が代入されます。

仮に、正規表現を /(.*)ball (.*)/ と変更して「Volleyball Player」という文字列をマッチさせた場合、
$1、$2には、それぞれ「Volley」「Player」が代入され、$3、$4には何も代入されません。

■構文・引数・戻り値

構文
RegExp.$1
RegExp.$2
RegExp.$3
RegExp.$4
RegExp.$5
RegExp.$6
RegExp.$7
RegExp.$8
RegExp.$9
戻り値
正規表現に含まれるn番目の括弧内のパターンにマッチした部分

■使用例

HTML + JavaScriptソース

<script>
var reObj = new RegExp(/(.*)(ball)(s)(.*)/);
reObj.test("Baseball News");
document.write("<h5>正規表現 /(.*)(ball)(s)(.*)/ で、文字列 Baseball News の場合</h5>");
document.write("<div>$1の値:「"  + RegExp.$1 + "」</div>");
document.write("<div>$2の値:「"  + RegExp.$2 + "」</div>");
document.write("<div>$3の値:「"  + RegExp.$3 + "」</div>");
document.write("<div>$4の値:「"  + RegExp.$4 + "」</div>");

reObj.test("Volleyball Player");
document.write("<h5>正規表現 /(.*)(ball)(s)(.*)/ で、文字列 Volleyball Player の場合</h5>");
document.write("<div>$1の値:「"  + RegExp.$1 + "」</div>");
document.write("<div>$2の値:「"  + RegExp.$2 + "」</div>");
document.write("<div>$3の値:「"  + RegExp.$3 + "」</div>");
document.write("<div>$4の値:「"  + RegExp.$4 + "」</div>");

var reObj = new RegExp(/(.*)ball (.*)/);
reObj.test("Volleyball Player");
document.write("<h5>正規表現 /(.*)ball (.*)/ で、文字列 Volleyball Player の場合</h5>");
document.write("<div>$1の値:「"  + RegExp.$1 + "」</div>");
document.write("<div>$2の値:「"  + RegExp.$2 + "」</div>");
document.write("<div>$3の値:「"  + RegExp.$3 + "」</div>");
document.write("<div>$4の値:「"  + RegExp.$4 + "」</div>");
</script>

↓↓↓

ブラウザ上の表示

“);
document.write(“

$2の値:「” + RegExp.$2 + “」

“);
document.write(“

$3の値:「” + RegExp.$3 + “」

“);
document.write(“

$4の値:「” + RegExp.$4 + “」

“);

reObj.test(“Volleyball Player”);
document.write(“

正規表現 /(.*)(ball)(s)(.*)/ で、文字列 Volleyball Player の場合

“);
document.write(“

$1の値:「” + RegExp.$1 + “」

“);
document.write(“

$2の値:「” + RegExp.$2 + “」

“);
document.write(“

$3の値:「” + RegExp.$3 + “」

“);
document.write(“

$4の値:「” + RegExp.$4 + “」

“);

var reObj = new RegExp(/(.*)ball (.*)/);
reObj.test(“Volleyball Player”);
document.write(“

正規表現 /(.*)ball (.*)/ で、文字列 Volleyball Player の場合

“);
document.write(“

$1の値:「” + RegExp.$1 + “」

“);
document.write(“

$2の値:「” + RegExp.$2 + “」

“);
document.write(“

$3の値:「” + RegExp.$3 + “」

“);
document.write(“

$4の値:「” + RegExp.$4 + “」

“);

Lorem Ipsum is simply dummy text

    By signing up, you agree to our Terms and Privacy Policy. Unsubscribe anytime.