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