..

JavaScript.StringObjec.replace

//StringObject.replace(/regexp/,newContent);
//1当newContent为新字符串,就直接用newContent对匹配的内容进行替换。
//2当newContent为函数的时候,就用函数返回的返回值对匹配的内容替换。
//3当不知到newContent为函数时候,传入的参数是啥,就用arguments.length,或打印arguments进行查看。


//基础的有
var words = "hello world";
var xx = words.replace("hello","hi");
console.log(words.replace("hello","hi"))




//当newContent为函数时候,函数的参数是这样的
- param 1: 匹配到的字符串
- param 2: 匹配的子字符串1
- param 3: 匹配的子字符串2
-...
 param m: 匹配的子字符串m
-...
- param n-1: 匹配到的字符串在字符串中的位置
- param n: 原始字符串
//一个牵强的例子
str = '13337663667';
str = str.replace(/(\d{3})(\d{4})(\d{4})/g,function(){
    console.log(arguments);
    return arguments[1]+'****'+arguments[3];
});
console.log(str);

 

posted @ 2018-11-15 15:57  罗浩楠  阅读(125)  评论(0)    收藏  举报
..