现在的位置: 首页 > 综合 > 正文

replace

2019年11月17日 ⁄ 综合 ⁄ 共 705字 ⁄ 字号 评论关闭

replace 方法@import url(../html-vss/msdnie4a.css);


replace 方法

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

stringObj.replace(
rgExp, replaceText)

参数

stringObj

必选项。要执行该替换的 String
对象或字符串文字。该字符串不会被 replace
方法修改。

rgExp

必选项。为包含正则表达式模式或可用标志的正则表达式
对象。也可以是 String
对象或文字。如果
rgExp
不是正则表达式
对象,它将被转换为字符串,并进行精确的查找;不要尝试将字符串转化为正则表达式。

replaceText

必选项。是一个String
对象或字符串文字,对于stringObj
中每个匹配
rgExp
中的位置都用该对象所包含的文字加以替换。在 Jscript 5.5 或更新版本中,replaceText

参数也可以是返回替换文本的函数。

 

示例:

将字符串“aa;bb;cc;”中的";"换成"空隔"

function myReplace(str,beReplace,newword){

     while(str.indexOf(beReplace)>0)

    {

          str=str.replace(beReplace,newword);

    }

    return str;

}

 

<span id="mySpan"></span>

<script type="text/javascript">
 

 document.getElementById("mySpan").innerHTML=myReplace("aa;bb;cc;",";"," ");
 
</script>

【上篇】
【下篇】

抱歉!评论已关闭.