一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

js字符替换函数的详细使用方法

时间:2011-05-25 编辑:简简单单 来源:一聚教程网

定义和用法
replace() 方法用于在字符串中用一些字符替换另一些字符,或替换一个与正则表达式匹配的子串。
语法
stringObject.replace(regexp/substr,replacement)
参数 描述
regexp/substr 
必需。规定子字符串或要替换的模式的 RegExp 对象。
请注意,如果该值是一个字符串,则将它作为要检索的直接量文本模式,而不是首先被转换为 RegExp 对象。
replacement 必需。一个字符串值。规定了替换文本或生成替换文本的函数。
返回值
一个新的字符串,是用 replacement 替换了 regexp 的第一次匹配或所有匹配之后得到的。

示例
   下面的示例演示了 replace 方法将第一次出现的单词 "The" 替换为单词 "A" 的用法。

   
   function ReplaceDemo(){
    var r, re; // 声明变量。
    var ss = "The man hit the ball with the bat.n";
    ss += "while the fielder caught the ball with the glove.";
    re = /The/g; // 创建正则表达式模式。
    r = ss.replace(re, "A"); // 用 "A" 替换 "The"。
    return(r); // 返回替换后的字符串。
   }
   另外, replace 方法也可以替换模式中的子表达式。 下面的范例演示了交换字符串中的每一对单词:
   
   function ReplaceDemo(){
    var r, re; // 声明变量。
    var ss = "The rain in Spain falls mainly in the plain.";
    re = /(S+)(s+)(S+)/g; // 创建正则表达式模式。
    r = ss.replace(re, "$3$2$1"); // 交换每一对单词。
    return

(r); // 返回结果字符串。
   }
   下面的示例(在 JScript 5.5 及更新版本中执行)执行的是从华氏到摄氏的转换,它演示了使用函数作为 replaceText。要想知道该函数是如何工作的,传递一个包含数值的字符串,数值后要紧跟 "F" (例如 "Water boils at 212")。 
   

   function f2c(s) {
    var test = /(d+(.d*)?)Fb/g; // 初始化模式。
    return(s.replace
    (test,
    function($0,$1,$2) {
    return((($1-32) * 5/9) + "C");
    }
    )
    );
   }
   document.write(f2c("Water freezes at 32F and boils at 212F."));

今天在读Qwrap的源码stringH时里边有个

复制代码 代码如下:
format: function(s, arg0) {
var args = arguments;
return s.replace(/{(d+)}/ig, function(a, b) {
return args[(b | 0) + 1] || '';
});
}

它的使用方式是:
alert(format("{0} love {1}.",'I','You'))//I love you
format的实现方式主要是用到了String对象的replace方法:

replace:返回根据正则表达式进行文字替换后的字符串的复制。

1.平时常用到的replace

复制代码 代码如下:
function ReplaceDemo(){
var r, re; // 声明变量。
var ss = "The man hit the ball with the bat.n";
ss += "while the fielder caught the ball with the glove.";
re = /The/g; // 创建正则表达式模式。
r = ss.replace(re, "A"); // 用 "A" 替换 "The"。
return(r); // 返回替换后的字符串。
}
ReplaceDemo(); //A man hit the ball with the bat. while the fielder caught the ball with the glove.

2.替换模式中的子表达式

复制代码 代码如下:
function ReplaceDemo(){
var r, re; // 声明变量。
var ss = "The rain in Spain falls mainly in the plain.";
re = /(S+)(s+)(S+)/g; // 创建正则表达式模式。
r = ss.replace(re, "$3$2$1"); // 交换每一对单词。
return(r); // 返回结果字符串。
}
document.write(ReplaceDemo()); //rain The Spain in mainly falls the in plain.

匹配正则的项:The rain,in Spain,falls mainly,in the;执行ss.replace(re, "$3$2$1")操作,完成单词位置的交换

$1匹配的是第一个(S+)

$2匹配的是(s+)

$3匹配的是第二个(S+)

3.replace第二个参数是function时

复制代码 代码如下:

function f2c(s){
var test = /(d+(.d*)?)Fb/g; // 初始化模式。
return(s.replace(test,function($0,$1,$2){return((($1-32)) + "C");}));
}
f2c("Water boils at 212F 3F .2F 2.2F .2");//Water boils at 180C -29C .-30C -29.8C .2

$0匹配 212F,3F,.2F,2.2F
$1匹配 212,3,.2,2.2
$2匹配 最后一个.2

说明
字符串 stringObject 的 replace() 方法执行的是查找并替换的操作。它将在 stringObject 中查找与 regexp 相匹配的子字符串,然后用 replacement 来替换这些子串。如果 regexp 具有全局标志 g,那么 replace() 方法将替换所有匹配的子串。否则,它只替换第一个匹配子串。
replacement 可以是字符串,也可以是函数。如果它是字符串,那么每个匹配都将由字符串替换。但是 replacement 中的 $ 字符具有特定的含义。如下表所示,它说明从模式匹配得到的字符串将用于替换

replace 对大小写不敏感



JavaScript Tutorial

热门栏目