2017-10-16 58 views
-1

我有很多產品說明,我必須從中切出材料詳細信息。例如:使用javascript從字符串中切出特定部分

var desc = "All over print design SoulCal branding badge 80% Polyester, 20% Elastane Machine washable Keep away from fire." 

function materialCutter(desc){ 
    // some logic here... 
    // var material = "80% Polyester, 20% Elastane" 

    return material; 
} 

我想我已經使用了「%」符號,但在這一點上說實話我被卡住了。

由於提前,

+0

'desc.match(/ \ d +%\ w +/G)' – Thomas

+0

你可以嘗試使用這個RegExp'/ \ b \ d +%\ s {1} \ w + \ s {1} \ b/g' – Krusader

+1

是否有任何規則/模式標記材料節的結尾? – user2887596

回答

1

隨着String.match()功能:

var desc = "All over print design SoulCal branding badge 80% Polyester, 20% Elastane Machine washable Keep away from fire.", 
 
    materials = desc.match(/\b\d+% \w+/g); 
 
\t 
 
console.log(materials);