ITeye博客

js中的字符串替换

simple_hui · 2009-10-29 14:43 · JavaScript · 前端界面 · 网络资源 · Web前端 · Markdown

replace 语法 stringObj.replace(rgExp, replaceText)

replace 方法的语法包括下述部分: 部分 描述 stringObj 必选项。要执行该替换的 String 对象或文字。该对象不会被 replace 方法修改。 rgExp 必选项。描述要查找的内容的一个正则表达式对象。 replaceText 必选项。是一个String 对象或文字,对于stringObj 中每个匹配 rgExp 中的位置都用该对象所包含的文字加以替换。 例如: 1.只能替换第一个“a”字母

2.可以替换所有“a”字母。其中g为全局标志

3.在引用函数中所使用的将图片代码替换成表情符号

将所有的换成^~mrgreen~^

   //引用调用函数
   function quote(name,id){
    var quoteMsg=document.getElementById(id).innerHTML;
    var content=document.getElementById("comment");
    quoteMsg=quoteMsg.replace(/<IMG src=\"http:\/\/hikeimg.appspot.com\/static\/images\/icon\/icon_/g, "^~").replace(/.gif\">/g, "~^");
    content.value='引用'+name+'<blockquote>'+quoteMsg+'</blockquote>';
    return true;
   }

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。