RegExp.$1--RegExp.$9

$1 至 $9 属性 (JavaScript)

1(共 1)对本文的评价是有帮助-评价此主题

返回在模式匹配期间找到的,所存储的最近的九个部分。 只读。

RegExp.$n 
RegExp

始终为全局 RegExp 对象。

n

1 至 9 之间的任意整数。

每当产生一个带括号的成功匹配时,$1...$9 属性的值就被修改。可以在一个正则表达式模式中指定任意多个带括号的子匹配,但只能存储最新的九个。

下面的示例执行正则表达式搜索。 它显示了全局 RegExp 对象中的匹配项和子匹配项。 子匹配项是 $1…$9 属性中包含的成功的带括号匹配项。 该示例还显示了由 exec 方法返回的数组中的匹配项和子匹配项。

JavaScript
var newLine = "<br />";

var re = /(\w+)@(\w+)\.(\w+)/g
var src = "Please send mail to george@contoso.com and someone@example.com. Thanks!"

var result;
var s = "";

// Get the first match.
result = re.exec(src);

while (result != null) {
    // Show the entire match.
    s += newLine;

    // Show the match and submatches from the RegExp global object.
    s += "RegExp.lastMatch: " + RegExp.lastMatch + newLine;
    s += "RegExp.$1: " + RegExp.$1 + newLine;
    s += "RegExp.$2: " + RegExp.$2 + newLine;
    s += "RegExp.$3: " + RegExp.$3 + newLine;

    // Show the match and submatches from the array that is returned
    // by the exec method.
    for (var index = 0; index < result.length; index++) {
        s +=  index + ": ";
        s += result[index];
        s += newLine;
    }

    // Get the next match.
    result = re.exec(src);
}
document.write(s);

// Output:
//  RegExp.lastMatch: george@contoso.com
//  RegExp.$1: george
//  RegExp.$2: contoso
//  RegExp.$3: com
//  0: george@contoso.com
//  1: george
//  2: contoso
//  3: com

//  RegExp.lastMatch: someone@example.com
//  RegExp.$1: someone
//  RegExp.$2: example
//  RegExp.$3: com
//  0: someone@example.com
//  1: someone
//  2: example
//  3: com
实际例子
Date()对象的事件格式化
Date.prototype.format = function (format) {
        
        var _self = new Date();
        
        
        var o = {
            "M+" : _self.getMonth() + 1, //month
            "d+" : _self.getDate(), //day
            "h+" : _self.getHours(), //hour
            "m+" : _self.getMinutes(), //minute
            "s+" : _self.getSeconds(), //second
            "q+" : Math.floor((_self.getMonth() + 3) / 3), //quarter
            "S" : _self.getMilliseconds() //millisecond
        }
        
        if (/(Y+)/.test(format)) {
            format = format.replace(RegExp.$1, (_self.getFullYear() + "").substr(4 - RegExp.$1.length));
        }
        
        for (var k in o) {
            if (new RegExp("(" + k + ")").test(format)) {
                format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
            }
        }
        return format;
    }

 

var testStr = new Date().format("YYYY年MM月dd日hh小时mm分ss秒");

在以下文档模式中受支持:Quirks、Internet Explorer 6 标准模式、Internet Explorer 7 标准模式、Internet Explorer 8 标准模式、Internet Explorer 9 标准模式和 Internet Explorer 10 标准模式。Windows 应用商店 应用程序中也支持此项。请参见版本信息

适用于RegExp 对象 (JavaScript)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值