0
我想從字符串中獲取數組編號。 例如:正則表達式從字符串中獲取數組編號
Input: "@[Anonymous:2] abccdef, @[Sales:5]"
Output: [2, 5]
請幫幫我。謝謝
我想從字符串中獲取數組編號。 例如:正則表達式從字符串中獲取數組編號
Input: "@[Anonymous:2] abccdef, @[Sales:5]"
Output: [2, 5]
請幫幫我。謝謝
我不知道你用什麼語言,但你可以發表評論,我很樂意爲你更新我的答案和幫助。 編輯:OP在下面的註釋中指定javascript。
@\[\w+:(\d+)\]
每場比賽的結果是capture group 1
您可以在regex.com
var s = "@[Anonymous:2] abccdef, @[Sales:5]"
var r = /@\[\w+:(\d+)\]/g
var m = r.exec(s)
var a = []
while (m != null) {
a.push(Number(m[1]))
m = r.exec(s)
}
console.log(a)
感謝檢查您輸入的正則表達式的評論! 使用JavaScript –
'@ \ [\ w +:(\ d +)\]'稍微好一些,因此捕獲整個數字(您當前的正則表達式將只捕獲兩位或更多位數字的最後一位數字) – ctwheels
@msoliman你有一個無限循環 – ctwheels