欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

js replace替換字符串同時(shí)替換多個(gè)方法

 更新時(shí)間:2018年11月27日 10:22:44   作者:伊澤瑞爾  
這篇文章主要介紹了js replace替換字符串同時(shí)替換多個(gè)方法 的相關(guān)資料,需要的朋友可以參考下

在實(shí)際開發(fā)中,經(jīng)常會(huì)遇到替換字符串的情況,但是大多數(shù)情況都是用replace替換一種字符串,本文介紹了如何使用replace替換多種指定的字符串,同時(shí)支持可拓展增加字符串關(guān)鍵字?!       ?/p>

let content = `<div id="article_content273475" class="article-content-wrap"> 
 <p><strong>宅是一種信仰。</strong><br></p>
 <p><br></p>
 <p>Wi-Fi + 床 = 低配宅。</p>
 <p><br></p>
 <p>Wi-Fi + 床 + 零食 + 網(wǎng)綜= 進(jìn)階版肥宅。</p>
 <p><br></p>
 <p>Wi-Fi + 床 + 零食 + 網(wǎng)綜 + 外賣 + 擼貓 = 人間天堂金不換宅。</p>
 <p><br></p>
 <p>移動(dòng)互聯(lián)網(wǎng)迅猛發(fā)展的當(dāng)下,“人間天堂金不換”版宅可以說(shuō)是當(dāng)下一些“9000歲”<span class="text-remarks" label="備注">(即“90后”和“00后”)</span>年輕人的生活常態(tài)了。</p>
         </div>`;
       let article = content.replace(/(\<img|\<p|\<article|\<\/article|\<header|\<\/header)/gi, function ($0, $1) {
       return {
        "<img": '<img style="width:100%;height:auto;display:block;" ',
        "<p": '<p style="text-indent: 24px;" ',
        "<article":"<div",
        "</article": "</div",
        "<header": "<div",
        "</header": "</div"
       }[$1];
      });
      console.log(article)

replace里的g表示全局替換,而每個(gè)關(guān)鍵詞前面的\則為轉(zhuǎn)義字符,在針對(duì)html類的標(biāo)簽替換的時(shí)候,是必不可少的。

下面看下js同時(shí)替換多個(gè)字符串的方法

<script>
  var s="my javascript is very poor,who can help me?"
  var reg=/(\w*)my(.*)is(.*)can(.*)/g
  alert(s.replace(reg,"$1his$2was$3could$4"));
</script>

總結(jié)

以上所述是小編給大家介紹的js replace替換字符串同時(shí)替換多個(gè)方法 ,希望對(duì)大家有所幫助,如果大家有所幫助,如果大家有任何疑問(wèn)歡迎給我留言,小編會(huì)及時(shí)回復(fù)大家的!

相關(guān)文章

最新評(píng)論