麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 編程 > JavaScript > 正文

JS中實現replaceAll的方法(實例代碼)

2019-11-20 21:43:54
字體:
來源:轉載
供稿:網友

第一次發現JavaScript中replace() 方法如果直接用str.replace("-","!") 只會替換第一個匹配的字符.
而str.replace(//-/g,"!")則可以全部替換掉匹配的字符(g為全局標志)。

replace()
The replace() method returns the string that results when you replace text matching its first argument
(a regular expression) with the text of the second argument (a string).
If the g (global) flag is not set in the regular expression declaration, this method replaces only the first
occurrence of the pattern. For example,

var s = "Hello. Regexps are fun.";s = s.replace(//./, "!"); // replace first period with an exclamation pointalert(s);

produces the string “Hello! Regexps are fun.” Including the g flag will cause the interpreter to
perform a global replace, finding and replacing every matching substring. For example,

var s = "Hello. Regexps are fun.";s = s.replace(//./g, "!"); // replace all periods with exclamation pointsalert(s);

yields this result: “Hello! Regexps are fun!”

所以可以用以下幾種方式.:
string.replace(/reallyDo/g, replaceWith);
string.replace(new RegExp(reallyDo, 'g'), replaceWith);

string:字符串表達式包含要替代的子字符串。
reallyDo:被搜索的子字符串。
replaceWith:用于替換的子字符串。

復制代碼 代碼如下:

<script type="text/javascript"> 
String.prototype.replaceAll = function(reallyDo, replaceWith, ignoreCase) { 
    if (!RegExp.prototype.isPrototypeOf(reallyDo)) { 
        return this.replace(new RegExp(reallyDo, (ignoreCase ? "gi": "g")), replaceWith); 
    } else { 
        return this.replace(reallyDo, replaceWith); 
    } 

</script> 

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 特级a欧美做爰片毛片 | 久久国产精品二区 | 五月天影院,久久综合, | 黄色成人av在线 | 国产午夜精品一区二区三区嫩草 | 成年性羞羞视频免费观看无限 | av在线成人 | av电影在线观看免费 | 午夜视频色 | 色综合欧美 | 污污短视频| 狠狠干视频网站 | 欧美大穴 | 亚洲一区 国产精品 | 在线免费日本 | 深夜福利视频绿巨人视频在线观看 | 成人羞羞在线观看网站 | 久久草草亚洲蜜桃臀 | 美女91视频 | 中国产一级毛片 | 欧美激情综合在线 | 国产精品久久久久国产精品三级 | 欧美精品毛片 | 亚洲av一级毛片特黄大片 | 91热久久免费频精品黑人99 | 欧美精品亚洲人成在线观看 | 圆产精品久久久久久久久久久 | 精品久久久久久久久中文字幕 | 啊~用cao嗯力cao烂我视频 | 欧美精品99 | 国产激情精品一区二区三区 | 成人免费国产 | 毛片视频网站在线观看 | 成人性视频在线 | 性 毛片 | 91精品国产99久久久久久红楼 | 午夜国产精品成人 | 一级做a爱视频 | 成人福利在线 | 91午夜在线观看 | 午夜视频在线看 |