---
title: "js 实现动态替换 占位符 和取context"
author: "weina"
date: "2011-07-19T10:53:11Z"
tags: []
categories: ["Web前端"]
slug: "js-context"
draft: false
lastmod: "2011-08-03T14:40:05Z"
id: 1127699
url: "https://weina.iteye.com/blog/1127699"
---

var MessageFormat = function(str){  
    var args = \[].splice.call(arguments,1);

//因为aruguments是个伪数组，没有数组的splice方法，所以，我们要用call给arguments只想一个aplice的方法，这样我们//就可以截取到 arguments后面的参数了  
    return str.replace(/\\{(\[^}])\\}/g,function(index,value){  
        return args\[value];

//下面我们有那个了replace，利用正则将我们捕获到的占位符，替换掉  
    })  
}

 

//取url中的context

var str ="http://my.tianji.com/dashboard/777";  
aa = str.match(/https?:\\/\\/\[^/]+\\/(\[^/]+)/);  
console.log(aa\[1])