我需要洗掉字串中的重復單詞,而不是數字字符。
這是我需要轉換并保留數字字符的文本示例。有關如何處理此問題的任何建議?
字串示例:“Rim - Rim Black 28H 700mm x 700mm”
static removeDuplicateWords = (statement: string) => {
return statement
.split(" ")
.filter((item, pos, self) => {
return self.indexOf(item) === pos;
})
.join(" ");
};
當前結果: - Rim Black 28H x 700mm
預期結果: - Rim Black 28H 700mm x 700mm
謝謝您的幫助
uj5u.com熱心網友回復:
使用正則運算式來測驗單詞是否包含數字。
static removeDuplicateWords = (statement: string) => {
return statement
.split(" ")
.filter((item, pos, self) => item.match(/\d/) || self.indexOf(item) === pos)
.join(" ");
};
uj5u.com熱心網友回復:
從您的示例中,您可以創建一組拆分字串并再次加入它。
console.log(Array.from(new Set("Rim - Rim Black 28H 700mm x 700mm".split(' '))).join(' '))
// print 'Rim - Black 28H x 700mm'
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/433698.html
標籤:javascript 节点.js 细绳 方法
