网站首页 美食营养 游戏数码 手工爱好 生活家居 健康养生 运动户外 职场理财 情感交际 母婴教育 时尚美容

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");

© 2025 五度知识库
信息来自网络 所有数据仅供参考
有疑问请联系站长 site.kefu@gmail.com