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

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

如何獲取字符串中雙下劃線之間的數(shù)字并將其與另一個數(shù)字相乘?

如何獲取字符串中雙下劃線之間的數(shù)字并將其與另一個數(shù)字相乘?

飲歌長嘯 2024-01-18 15:50:09
假設我有一個示例字符串,let str = "Congrats! ID: 342, your salary is increased by __5%__ and it will increase by __10%__ next month.";我需要獲取雙下劃線之間的數(shù)字(例如: __5%__ 和 __10%__ 如上所示)并將它們乘以 2。所以我的輸出應該是,let result = "Congrats! ID: 342, your salary is increased by __10%__ and it will increase by __20%__ next month.";注意:數(shù)字 342 應保持不變,因為它不在雙下劃線之間。我怎樣才能得到這個?提前致謝。
查看完整描述

3 回答

?
慕姐8265434

TA貢獻1813條經驗 獲得超2個贊

您可以使用String.replace()這樣的回調函數(shù):


let str = "Congrats! ID: 342, your salary is increased by __5%__ and it will increase by __10%__ next month.";


let res=str.replace(/__(\d+)%__/g,function(_,num){return "__"+(2*num)+"%__"});

console.log(res)


查看完整回答
反對 回復 2024-01-18
?
滄海一幻覺

TA貢獻1824條經驗 獲得超5個贊

您可以按模式“__”拆分 str。

檢查str是否以“%”結尾,如果是,則乘以兩次即可。

并以相同的模式再次加入數(shù)組。

  str

  .split("__")

  .map(maybeNumber => {

    if (maybeNumber.endsWith("%")) {

      return `${parseInt(maybeNumber) * 2}%`

    }


    return maybeNumber;

  })

  .join("__")


查看完整回答
反對 回復 2024-01-18
?
狐的傳說

TA貢獻1804條經驗 獲得超3個贊

var str = "Congrats! ID: 342, your salary is increased by __5%__ and it will increase by __10%__ next month.";


var idInString = str.match(/\d+/)[0];

str = str.replace(idInString,""); // result is "Congrats! ID: , your salary is increased by __5%__ and it will increase by __10%__ next month." Now, with the first number at the beginning of string. Now, we can use the same method to get the other two numbers.


var firstNumberInString = str.match(/\d+/)[0];

document.write(firstNumberInString*2+"%"); // results in "10%"


str = str.replace(firstNumberInString,""); 


var secondNumberInString = str.match(/\d+/)[0];

document.write(secondNumberInString*2+"%"); // results in "20%"


查看完整回答
反對 回復 2024-01-18
  • 3 回答
  • 0 關注
  • 184 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號