Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/371.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 匹配字符串中的参数_Javascript_Regex - Fatal编程技术网

Javascript 匹配字符串中的参数

Javascript 匹配字符串中的参数,javascript,regex,Javascript,Regex,我有一个字符串,如下所示 var x = "test/foo/4/bar/3" 我有一个模式 var y = "test/foo/{id}/bar/{age}" 是否可以通过正则表达式使用模式从变量“x”中提取数字4和3?我建议,如果字符串格式可以预测为该格式,则应避免使用正则表达式: var str = "test/foo/4/bar/3", parts = str.split('/'), id = parts[2], age = parts[4]; 但是,如果

我有一个字符串,如下所示

var x = "test/foo/4/bar/3"
我有一个模式

var y = "test/foo/{id}/bar/{age}"

是否可以通过正则表达式使用模式从变量“x”中提取数字4和3?

我建议,如果字符串格式可以预测为该格式,则应避免使用正则表达式:

var str = "test/foo/4/bar/3",
    parts = str.split('/'),
    id = parts[2],
    age = parts[4];

但是,如果您觉得确实必须使用正则表达式(并使您的生活复杂化),则有可能:

var str = "test/foo/4/bar/3",
    parts = str.split('/'),
    id = str.match(/\/(\d+)\//)[1],
    age = str.match(/\/(\d+)$/)[1];
console.log(id,age);

参考资料:


我建议,如果字符串格式可以预测为该格式,请避免使用正则表达式:

var str = "test/foo/4/bar/3",
    parts = str.split('/'),
    id = parts[2],
    age = parts[4];

但是,如果您觉得确实必须使用正则表达式(并使您的生活复杂化),则有可能:

var str = "test/foo/4/bar/3",
    parts = str.split('/'),
    id = str.match(/\/(\d+)\//)[1],
    age = str.match(/\/(\d+)$/)[1];
console.log(id,age);

参考资料:


如果您有非常量模式,并且您的模式是“test/name1/value1/name2/value2”,请检查以下内容:

function match(url, variables) {
    /* regexp */
    function MatchRE(variable) {
        return new RegExp("/" + variable + "\\/([^\\/]+)");
    }

    var regExp, result = {};
    for (var i=0; i<variables.length; i++) {
        regExp = new MatchRE(variables[i]);
        if (regExp.test(url)) {
            result[variables[i]] = RegExp.$1;
        }
    }
    return result;
}

example:
    match("test/foo/4/bar/3", ["foo", "bar"]); //Object {foo: "4"}
    match("test/foo/testValue1/bar/testValue2/buz/testValue3", ["foo", "buz"]); //Object {foo: "testValue1", buz: "testValue3"}
函数匹配(url、变量){
/*正则表达式*/
函数MatchRE(变量){
返回新的RegExp(“/”+变量+“\\/([^\\/]+)”);
}
var regExp,result={};

对于(var i=0;i如果您有非常量模式,并且您的模式是“test/name1/value1/name2/value2”,请检查以下内容:

function match(url, variables) {
    /* regexp */
    function MatchRE(variable) {
        return new RegExp("/" + variable + "\\/([^\\/]+)");
    }

    var regExp, result = {};
    for (var i=0; i<variables.length; i++) {
        regExp = new MatchRE(variables[i]);
        if (regExp.test(url)) {
            result[variables[i]] = RegExp.$1;
        }
    }
    return result;
}

example:
    match("test/foo/4/bar/3", ["foo", "bar"]); //Object {foo: "4"}
    match("test/foo/testValue1/bar/testValue2/buz/testValue3", ["foo", "buz"]); //Object {foo: "testValue1", buz: "testValue3"}
函数匹配(url、变量){
/*正则表达式*/
函数MatchRE(变量){
返回新的RegExp(“/”+变量+“\\/([^\\/]+)”);
}
var regExp,result={};
对于(var i=0;i)字符串是否始终采用这种形式?五个组件,用斜线分隔?字符串是否始终采用这种形式?五个组件,用斜线分隔?