第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

用多個其他字符串替換多個字符串

用多個其他字符串替換多個字符串

用多個其他字符串替換多個字符串我試圖用多個其他單詞替換字符串中的多個單詞。繩子是“我有一只貓,一只狗,一只山羊?!比欢?,這并不產(chǎn)生“我有一只狗,一只山羊,一只貓”,而是產(chǎn)生“我有一只貓,一只貓”。是否可以在JavaScript中同時將多個字符串替換為多個其他字符串,從而產(chǎn)生正確的結(jié)果?var str = "I have a cat, a dog, and a goat.";str = str.replace(/cat/gi, "dog"); str = str.replace(/dog/gi, "goat");str = str.replace(/goat/gi, "cat"); //this produces "I have a cat, a cat, and a cat"//but I wanted to produce the string "I have a dog, a goat, and a cat".
查看完整描述

3 回答

?
繁華開滿天機(jī)

TA貢獻(xiàn)1816條經(jīng)驗 獲得超4個贊

這對我有用:

String.prototype.replaceAll = function(search, replacement) {
    var target = this;
    return target.replace(new RegExp(search, 'g'), replacement);};function replaceAll(str, map){
    for(key in map){
        str = str.replaceAll(key, map[key]);
    }
    return str;}//testing...var str = "bat, ball, cat";var map = {
    'bat' : 'foo',
    'ball' : 'boo',
    'cat' : 'bar'};var new = replaceAll(str, map);//result: "foo, boo, bar"


查看完整回答
反對 回復(fù) 2019-07-10
  • 3 回答
  • 0 關(guān)注
  • 1048 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號