在js中的replace方法详解
发布时间:2007-08-21 15:47:00
replace 方法
返回根据正则表达式进行文字替换后的字符串的复制。
stringObj.replace(rgExp, replaceText)
参数
stringObj
必选项。要执行该替换的 String 对象或字符串文字。该字符串不会被 replace 方法修改。
rgExp
必选项。为包含正则表达式模式或可用标志的正则表达式对象。也可以是 String 对象或文字。如果 rgExp 不是正则表达式对象,它将被转换为字符串,并进行精确的查找;不要尝试将字符串转化为正则表达式。
replaceText
必选项。是一个String 对象或字符串文字,对于stringObj 中每个匹配 rgExp 中的位置都用该对象所包含的文字加以替换。在 Jscript 5.5 或更新版本中,replaceText 参数也可以是返回替换文本的函数。
说明
replace 方法的结果是一个完成了指定替换的 stringObj 对象的复制。下面任意的匹配变量都能用来识别最新的匹配以及找出匹配的字符串。在需要动态决定替换字符串的文本替换中可以使用匹配变量。
字符 | 含义 |
---|---|
$$ | $ (JScript 5.5 或更新版本) |
$& | 指定与整个模式匹配的 stringObj 的部分。(JScript 5.5 或更新版本) |
$` | 指定由 $& 描述的匹配之前的 stringObj 部分。(JScript 5.5 或更新版本) |
$' | 指定由 $& 描述的匹配之后的 stringObj 部分。(JScript 5.5 或更新版本) |
$n | 捕获的第 n 个子匹配,此处 n 为从1到9的十进制一位数。(JScript 5.5 或更新版本) |
$nn | 捕获的第 nn 个子匹配,此处 nn 为从01到99的十进制两位数。(JScript 5.5 或更新版本) |
如果 replaceText 为函数,对于每一个匹配的子字符串,调用该函数时带有下面的 m+3 个参数,此处 m 是在 rgExp 中捕获的左括弧的个数。第一个参数是匹配的子字符串。接下来的 m 个参数是查找中捕获的全部结果。第 m+2 个参数是在 stringObj 中匹配出现的偏移量,而第 m+3 个参数为 stringObj。结果为将每一匹配的子字符串替换为函数调用的相应返回值的字符串值。
Replace 方法更新全局 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*)?)F\b/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."));
function($0,$1,$2) {
return((($1-32) * 5/9) + "C");
}
)
);
}
document.write(f2c("Water freezes at 32F and boils at 212F."));
自己写的两个方法:
<html><SCRIPT LANGUAGE="JavaScript"><!--function replaceAll(str,b){//var renStr=str.replace("-","*");这样写只能替换第一个匹配的字符.var renStr=str.replace(/\-/g,b);//这样写可以替换匹配的字符所有字符.与replaceAllNew()方法作用是一样的.document.all.text4.value=renStr;document.all.text2.value="";document.all.text3.value="";}function replaceAllNew(text,replacement,target){ if(text==null || text=="") return text;//如果text无内容,返回text if(replacement==null || replacement=="") return text;//如果replacement无内容,返回text if(target==null) target="";//如果target无内容,设置为空串 var returnString="";//定义返回值变量,并初始化 var index=text.indexOf(replacement);//定义查找replacement的索引下标,并进行第一次查找 while(index!=-1) {//直至未找到replacement,要么进行下面的处理 //alert(index); //alert(returnString); returnString+=text.substring(0,index)+target;//如果找到的replacement前有字符,累加到返回值中,并加上target text=text.substring(index+replacement.length);//取掉找到的replacement及前边的字符 index=text.indexOf(replacement);//进行查询,准备下一次处理 } if(text!="") returnString+=text;//如果找到的最后一个replacement后有字符,累加到返回值中 //alert(returnString);document.all.text4.value=returnString;document.all.text2.value="";document.all.text3.value="";return returnString;//返回 } function bodyOnload(){var str1="adbddasd";//replaceAll(str1,’a’,’A’);}//--></SCRIPT><body >1:javascript 中的replace方法测试!<br>原来的:<input type="text" value="">;转换后的:<input type="text" value=""><br>把字符:<input type="text" value=""><br>替换为:<input type="text" value=""><br><input type="button" value="replace1" /><input type="button" value="replace2" /></body></html>
猜你喜欢
- 简介:本文主要介绍在linux系统下,如何配置mysql支持IPV6的连接。环境要求:1、debian7.5操作系统虚拟机2、mysql5.
- Python使用贪婪算法解决问题集合覆盖问题假设你办了个广播节目,要让全美50个州的听众都收听到。为此,你需要决定在哪些广播台播出。在每个广
- 任务1、编写程序实现乐手弹奏乐器多态class Instrument(): def make_sound(self
- 摘要:现代网站和web应用程序趋向于依赖客户端的大量的javascript来提供丰富的交互。特别是通过不刷新页面的异步请求来返回数据或从服务
- 回顾一下已经了解的数据类型:int/str/bool/list/dict/tuple还真的不少了.不过,python是一个发展的语言,没准以
- 1.找到配置文件-打开“开始菜单--Anaconda3文件夹--Anaconda Prompt”-输入命令: jupyter noteboo
- 系统自带模块(库)```cppimport retarget = 'abc1234xyz're.search('(\
- 1、自定义 图表 组件Echarts.vue<!-- 自定义 echart 组件 --><template> <
- 正则表达式gi我刚开始也看不懂,从网上找到后看到了,现在分享给大家正在表达式的通项: /pattern/flags 即(/模式/标记 )构造
- 一、慢查询日志概念对于SQL和索引的优化问题,我们会使用explain去分析SQL语句。但是真正的企业级项目有成千上万条SQL,我们不可能从
- 虽然淘宝商城的名字中带有“商城”两字,但是很显然的,淘宝商城并不是一个B2C商城,淘宝商城仍只是一个C2C平台,充其量只是个收费版的淘宝。在
- 1.DQL类型的SQL语句基本概述DQL类型的SQL语言全称为Data Query Language,中文名称为数据查询语言,主要是用来查询
- Python中的is和==比较两个对象的两种方法在Python中有两种方式比较两个对象是否相等,分别是is和==,两者之间是不同的==比较的
- 注意:myemployees库和shoppingCart库在同一台物理主机,如果不在同一台物理主机该怎么办呢?下面我会介绍到。情况一2个库在
- 如何在页面错误时向数据库中添加记录?在ASP中,我在页面中添加了On Error Resume Next错误命令,以消除和避免程序错误。现在
- python标记语句块使用方法,python语言和其它的编程语言有着显著的区别,那就是python对格式的要求非常苛刻,好处就是书写上简易命
- mmdetection简介 目标检测工具箱mmdetection是香
- 要想从命令行启动mysqld服务器,你应当启动控制台窗口(或“DOS window”)并输入命令:C
- 我正在开发某种RESTful API.发生一些错误时,我会抛出一个App :: abort($code,$message)错误.问题是:我希
- IE历来被web标准的拥护者所诟病,而当FireFox横空出世以后,更多的网页制作者开始关注web标准设计。看着FireFox的市场占有率不