JavaScript 中的replace方法
来源:互联网 发布:在linux中创建文件 编辑:程序博客网 时间:2024/05/01 12:19
1第一次发现JavaScript中replace() 方法如果直接用str.replace("-","!") 只会替换第一个匹配的字符.
2而str.replace(//-/g,"!")则可以替换掉全部匹配的字符(g为全局标志)。
3
4
5replace()
6The replace() method returns the string that results when you replace text matching its first argument
7(a regular expression) with the text of the second argument (a string).
8If the g (global) flag is not set in the regular expression declaration, this method replaces only the first
9occurrence of the pattern. For example,
10
11var s = "Hello. Regexps are fun.";s = s.replace(//./, "!"); // replace first period with an exclamation pointalert(s);
12
13produces the string “Hello! Regexps are fun.” Including the g flag will cause the interpreter to
14perform a global replace, finding and replacing every matching substring. For example,
15
16var s = "Hello. Regexps are fun.";s = s.replace(//./g, "!"); // replace all periods with exclamation pointsalert(s);
17
18yields this result: “Hello! Regexps are fun!”
2而str.replace(//-/g,"!")则可以替换掉全部匹配的字符(g为全局标志)。
3
4
5replace()
6The replace() method returns the string that results when you replace text matching its first argument
7(a regular expression) with the text of the second argument (a string).
8If the g (global) flag is not set in the regular expression declaration, this method replaces only the first
9occurrence of the pattern. For example,
10
11var s = "Hello. Regexps are fun.";s = s.replace(//./, "!"); // replace first period with an exclamation pointalert(s);
12
13produces the string “Hello! Regexps are fun.” Including the g flag will cause the interpreter to
14perform a global replace, finding and replacing every matching substring. For example,
15
16var s = "Hello. Regexps are fun.";s = s.replace(//./g, "!"); // replace all periods with exclamation pointsalert(s);
17
18yields this result: “Hello! Regexps are fun!”
- javascript 中的replace方法
- JavaScript 中的replace方法
- javascript 中的replace方法
- javascript中的replace方法
- javascript replace 方法与java中的replace方法
- javascript中的replace方法的使用
- 深入理解 JavaScript 中的 replace 方法
- 深入理解 JavaScript 中的 replace 方法
- javascript中的replace()函数
- javascript中replace()方法
- javascript replace方法
- JavaScript replace() 方法
- JavaScript replace() 方法
- JavaScript replace() 方法
- JavaScript replace() 方法
- Javascript中replace方法
- JavaScript replace() 方法
- JavaScript replace() 方法
- GRUB的使用(三)
- 安理会席位暗流涌动,争端再起
- ASP小偷(远程数据获取)程序的入门教程
- 数字信号处理
- 意志与毅力
- JavaScript 中的replace方法
- JSP避免Form重复提交的三种方案
- JSP/Servlet应用程序优化八法
- GRUB的使用 (四)
- 建立JSP操作以提高数据库访问的效率
- JavaBean实现多文件上传的两种方法摘要:
- 应用程序也能变成“系统服务”
- 我正在想你.....
- 纵论5-7年内的技术发展大势 (Robbin)