我有以下字符串:的Javascript:格式字符串和避免多次更換
var str='
<span class="productName">Basa fillets</span><br>
Brand:
<span class="brandName">COMPLIMENTS</span><br>
400 <abbr title="Gram" lang="en">gr</abbr>
'
我需要「400」(可以是一個詞,甚至一個句子)。 我至今是:
d = str.replace(/<br>/g,'').replace(/<.*<\/.*>/g,'').replace(/\n/g,'').replace(/ */g,'').replace(/brand:/i,'');
它的工作原理,但......嗯,我敢肯定,我可以做的更好。我有很多類似的排隊替換我的代碼,我想知道如何改善,所以我更尋找一個比一個特定的解決方案一般的答案。
謝謝!
您的字符串無效,您需要用'\「'或''''在主字符串中使用''''符號。將其更改爲'var str =「巴薩魚片 gr」 ;' –
abc123
品牌: COMPLIMENTS
400
或者用「'改變它周圍的」「。編輯,顯然是一個錯字... – xShirase