时间: 2021-07-31 作者:daque
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, 1011var s = "hello. regexps are fun.";s = s.replace(/\./, "!"); // replace first period with an exclamation pointalert(s); 1213produces 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, 1516var s = "hello. regexps are fun.";s = s.replace(/\./g, "!"); // replace all periods with exclamation pointsalert(s); 1718yields this result: “hello! regexps are fun!”