2017-06-06 118 views
-1
"5 Teams 7 Players" ====> 5Teams 7Players 
"7 Individuals 8 Teams 8 Categories" ====> 7Individuals 8Teams 8Categories 
+5

哪裏是你的代碼,使遠嗎?你卡在哪裏? – smarx

+1

爲什麼'5隊7球員'減少到'5Teams7Players'?你有什麼嘗試?你嘗試時遇到什麼問題? – Tezra

+0

你的例子沒有反映你的標題;你的例子在數字和字母之間仍然有空格。 –

回答

0

之間的所有空間試試這個模式(\d+)(\s+)(\w+)

Demo regex

var a="5 Teams 7 Players"; 
 
var b="7 Individuals 8 Teams 8 Categories"; 
 

 
console.log(a.replace(/(\d+)(\s+)(\w+)/g,'$1$3')) 
 
console.log(b.replace(/(\d+)(\s+)(\w+)/g,'$1$3'))

+1

我更喜歡'a.replace(/(\ d)([a-z])/ ig,「$ 1 $ 2」)'。目前還不清楚OP是否想要匹配多個空間,更不用說其他類型的空白了。此外,沒有理由匹配多個數字或多個字母,我認爲'[a-z]'是比'\ w'更好的「字母」定義。 – smarx

0

兩個普拉薩德的和smarx的模式可以進一步簡化。只需尋找一個數字,然後留出空間,並在更換空間上留出空間。

var a="5 Teams 7 Players"; 
 
var b="7 Individuals 8 Teams 8 Categories"; 
 

 
console.log(a.replace(/(\d) /g,'$1')) 
 
console.log(b.replace(/(\d) /g,'$1'))

輸出(如果你不想單擊運行):

5Teams 7Players 
7Individuals 8Teams 8Categories 
相關問題