js中字符替换函数String.replace()使用技巧

 时间:2024-11-11 05:22:58

1、例子 1在本例中,我们将使用 "jb51.net" 替换字符串中的 "Microsoft":<script type="text/javascript">var str="Visit Microsoft!"document.write(str.replace(/Microsoft/, "jb51.net"))</script>输出:Visit jb51.net!

2、例子 2在本例中,我们将执行一次全局替换,每当 "Microsoft" 被找到,它就被替换为 "jb51.net":<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, "jb51.net"))</script>输出:Welcome to jb51.net! We are proud to announce that jb51.net has one of the largest Web Developers sites in the world.

3、例子 3您可以使用本例提供的代码来确保匹配字符串大写字符的正确:text = "javascript Tutorial";text.replace(/javascript/i, "JavaScript");

  • Excel中怎么用replace函数替换文本
  • search and replace string的一种用法
  • EXCEL中REPLACE函数的使用技巧实例
  • 如何使用Excel中的replace函数?
  • 使用java中replaceAll方法替换字符串中的反斜杠
  • 热门搜索
    怎么祛黄褐斑 输入法没了怎么弄出来 电脑开机慢怎么解决 魂印星斗盘攻略 肖邦简介 东钱湖旅游攻略 我的世界巨人怎么做 怎么治过敏性鼻炎 菩提子怎么盘玩 眼睛酸涩是怎么回事