定义和用法

replace() 方法用于在字符串中用一些字符替换另一些字符,或替换一个与正则表达式匹配的子串。

语法

stringObject.replace(regexp/substr,replacement)

实例

例子 1

在本例中,我们将使用 "W3School" 替换字符串中的 "Microsoft":

<script type="text/javascript">

var str="Visit Microsoft!"
document.write(str.replace(/Microsoft/, "W3School")) </script>

输出:

Visit W3School!

例子 2

在本例中,我们将执行一次全局替换,每当 "Microsoft" 被找到,它就被替换为 "W3School":

<script type="text/javascript">

var str="Welcome to Microsoft! "
str=str + "We are proud to announce that Microsoft has "
str=str + "one of the largest Web Developers sites in the world." document.write(str.replace(/Microsoft/g, "W3School")) </script>

输出:

Welcome to W3School! We are proud to announce that W3School
has one of the largest Web Developers sites in the world.

参考:

http://www.w3school.com.cn/jsref/jsref_replace.asp

原文地址:

https://www.cnblogs.com/poterliu/p/11009330.html

最新文章

  1. 爬虫技术 -- 进阶学习(十)网易新闻页面信息抓取(htmlagilitypack搭配scrapysharp)
  2. information_schema系列五(表,触发器,视图,存储过程和函数)
  3. ASP.NET编程模型之ASP.NET页面生命周期图解
  4. JS open App(未安装就跳转下载页面)
  5. ML 05、分类、标注与回归
  6. 无缝漫游 Seamless Roaming
  7. Ubuntu Vim 复制到系统粘贴板
  8. jQuery中怎么添加innerText、innerHtml(转)
  9. 【Android Studio使用教程6】Execution failed for task &#39;:&#215;&#215;&#215;:compileReleaseAidl&#39;
  10. 使用Java反射(Reflect)、自定义注解(Customer Annotation)生成简单SQL语句
  11. CSS sprite 圆角——源代码
  12. write &amp; read a MapFile(基于全新2.2.0API)
  13. canvas之----浮动小球
  14. Cannot open your terminal &#39;/dev/pts/4&#39; - please check.
  15. Meth | elementary OS常用配置
  16. SOCKET网络编程细节问题(2)
  17. 【Time系列四】查询各月份的日历
  18. Pycharm选择pyenv安装的Python版本
  19. sql sever基本语法总结
  20. [LTR] 信息检索评价指标(RP/MAP/DCG/NDCG/RR/ERR)

热门文章

  1. iphone状态栏,导航栏,标签栏高度一览表
  2. Python web前端 08 字符串 数组 json
  3. mac下配置php+mysql+nginx遇到的问题
  4. 下一代的前端构建工具:parcel打包react
  5. mysql 中unsigned
  6. java编程基础二进制
  7. dubbo注解
  8. SlickEdit 18.0 版本发布 同时更新破解文件
  9. LoadRunner问题解决
  10. pc端常见布局---水平居中布局 单元素不定宽度