我有這樣的字串
total sales 234 rs
total cost 651 rs
我只想得到
最終結果應該是這樣的
total sales
total cost
我怎么能得到請幫忙謝謝
uj5u.com熱心網友回復:
假設您總是想在第一個數字之前將其切斷,您可以找到第一個數字的索引:
const index = 'total sales 234 rs'.search(/\d/);
然后只需獲取子字串:
substring(0, index - 1);
uj5u.com熱心網友回復:
你可以這樣做:
let str = 'total sales 234 rs';
let result = str.split(' ').slice(0,2).join(' ');
uj5u.com熱心網友回復:
假設字串可以包含兩個以上的主要單詞 For egTotal goods cost 200 rs
然后使用以下功能
function reducer(str) { // where str is the initial string
const modifiedStr = str.split(' ').reduce((acc, substr) => {
if(!parseInt(substr, 10) && substr !== 'rs') acc =substr ' ';
return acc;
}, "");
return modifiedStr.slice(0,modifiedStr.length - 2);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/436144.html
標籤:javascript 细绳 反应式
