JS实现文本中查找并替换字符

效果图:

代码如下,复制即可使用:

<!DOCTYPE html>
<html>
<head>
  <style type="text/css">
    *{font-family:"微软雅黑";font-size:16px;margin:0;padding:0;letter-spacing:3px;line-height:22px;}
    #wrap{width:500px;height:300px;margin:20px auto;position:relative;}
    #text{width:500px;height:300px;border:1px solid green;}
    #result{width:500px;position:absolute;left:0px;top:0px;display:none;}
    input{width:100px;height:40px;margin-top:5px;}
    span{background-color:yellow;}
  </style>
</head>
<body>
  <div id="wrap">
    <div id="result"></div>
    <textarea id="text" placeholder="请在此输入文本。。。。"></textarea>
    <input type="text" id="val" placeholder="被查找字符"/>——<input type="text" id="new_val" placeholder="替换后字符"/>&nbsp;&nbsp;&nbsp;
    <input type="button" id="btnFind" value="替换"/>&nbsp;&nbsp;&nbsp;<input type="button" value="重置" id="reset"/>
  </div>
  <script type="text/javascript">   function replace(){
  var inner=null;
  var txt=document.getElementById('text').value.toString();
   var val=document.getElementById('val').value.toString();
   var newVal=document.getElementById('new_val').value.toString();
  newInner="<span>"+newVal+"</span>";
   for(var i=0;i<=txt.length-val.length;i++){
   if(txt==""||val=="") {
   alert("请输入内容!");
   return false;
   }
   if(txt.length<val.length) {
   return false;
   }
  if(i+val.length>=txt.length){
   if(txt.substring(i)==val){
   inner=txt.split('');
   inner.splice(i,val.length,newInner);
   txt=inner.join("");
   i=i+newInner.length-1;
   }    }
   else {
  if(txt.substring(i,i+val.length)==val) {
   inner=txt.split('');
   inner.splice(i,val.length,newInner);
   txt=inner.join("");
   i=i+newInner.length-1;
   }
  }
   }
   document.getElementById("result").innerHTML=txt;
  document.getElementById("text").value=null;
   document.getElementById("text").placeholder="";
   document.getElementById("result").style.display="block";
  }   document.getElementById('btnFind').onclick=function(){
  replace();
  }
  document.getElementById('reset').onclick=function(){
   document.getElementById("result").style.display="none";
  document.getElementById("val").value=null;
  document.getElementById("new_val").value=null;
   document.getElementById("text").placeholder="请在此输入文本。。。。";   }   </script>
</div>
</body>
</html>

如有错误,欢迎联系我改正,非常感谢!!!

JS实现文本中查找并替换字符的更多相关文章

  1. C# 在word中查找及替换文本

    C# 在word中查找及替换文本 在处理word文档时,很多人都会用到查找和替换功能.尤其是在处理庞大的word文档的时候,Microsoft word的查找替换功能就变得尤为重要,它不仅能让我们轻易 ...

  2. 【python cookbook】【字符串与文本】6.以不区分大小写的方式对文本做查找和替换

    问题:以不区分大小写的方式对文本做查找和替换 解决方法:使用re模块,并对各种操作都添加上re.IGNORECASE标记 text='UPPER PYTHON,lower python,Mixed P ...

  3. Linux中在vim/vi模式下对文本的查找和替换

    查找: 1.vim  filename  进入一般模式下 2.查找和替换方法 /word    向下查找word 的字符串  例如  /chengtingting   向下查找字符chengtingt ...

  4. C# 在excel中查找及替换数据

    在使用Excel处理数据时,有时候工作表内容很多,如果手动地一行一行的找数据很难发现它们在哪个地方.微软Excel给我们提供了一个很强大的数据处理功能-查找和替换,通过这个功能,我们可以快速地找到想要 ...

  5. [LeetCode] Find And Replace in String 在字符串中查找和替换

    To some string S, we will perform some replacement operations that replace groups of letters with ne ...

  6. [转]grep 在文本中查找内容

    转自: http://www.lampweb.org/linux/3/27.html 功能:grep系列是Linux中使用频率最高的文本查找命令.主要功能在一个或者多个文件中查找特定模式的字符串.如果 ...

  7. 使用SQL除掉文本中特殊的ascll字符比如Enter,Tab,空格键

    一.在SQL查询的字段中如果包含tab.enter.空格键,可以使用ascii码进行替换: --替换了文本中含有tab键,Enter键,空格键的ascii码 select REPLACE(REPLAC ...

  8. Sublime 、NotePad++中查找匹配中文字符

    在Sublime .NotePad++中可以使用正则表达式 [\x{4e00}-\x{9fa5}] 查找匹配中文字符.

  9. 在LoadRunner中查找和替换字符串

    参考<Search & Replace function for LoadRunner>: http://ptfrontline.wordpress.com/2009/03/13/ ...

随机推荐

  1. Linux 下安装nginx的总结 (之前写的有问题))

    1. 下载niginx的 tar包 下载路径 http://nginx.org/en/download.html 也可以直接使用命令下载 wget http://nginx.org/download/ ...

  2. swagger error: Conflicting schemaIds: Duplicate schemaIds detected for types A and B

    使用Web API并使用swashbuckle生成swagger文档,我在两个不同的命名空间中定义了两个具有相同名称的不同类.当我在浏览器中打开swagger页面时,它说: Conflicting s ...

  3. [代码]--c#获取系统时间

    public DateTime GetServerDataTime() { string sql = " select top 1 getdate() from sysobjects &qu ...

  4. suoi22 WRX知识树(dfs序)

    把一条路径拆成到根的四个链(两端点.lca和fa[lca]),然后给dfs序中链的端点做单点修改.区间查询它的子树和再加上它原来的权值就可以了 #include<bits/stdc++.h> ...

  5. codeblocks编译器

    发现网络有些编译器没有MinGW,特此留一文件: https://pan.baidu.com/s/1pLltzvH 有时下载codeblocks后编译不了,还要修改MinGW的位置,找到MinGW文件 ...

  6. git开发部署流程

    git的分支操作 https://blog.csdn.net/QH_JAVA/article/details/77853605 Git 开发部署流程 采用业界成熟方案 Git Flow 分支方式进行开 ...

  7. SSO系统的实现

    当一个网站系统比较大型的时候,我们通常采用面向服务的编程,采用分布式的编程.各个子系统共同来实现一个大的系统,这时候登录注册功能的实现也面临着一些问题. 一.WHAT? SSO是什么? sso是单点登 ...

  8. Java基础-处理json字符串解析案例

    Java基础-处理json字符串解析案例 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 作为一名开发人员,想必大家或多或少都有接触到XML文件,XML全称为“extensible ...

  9. bzoj千题计划210:bzoj2642 | Poj3968 | UVALive 4992| hdu 3761 Jungle Outpost

    http://www.lydsy.com/JudgeOnline/problem.php?id=2642 题意: n个瞭望台,形成一个凸n边形.这些瞭望台的保护范围是这个凸包内的任意点. 敌人进攻时, ...

  10. 何凯文每日一句打卡||DAY7