我想從url中獲取文件名。我這樣做:獲取無法讀取正則表達式中null的屬性'0'
console.log(url)
const filename = url.match(/([^\/]+)(?=\.\w+$)/)[0]
return filename
但我得到TypeError: Cannot read property '0' of null
。這很奇怪,因爲console.log(url)
輸出了file: http://ac-0uhksb6K.clouddn.com/9710016c8dfcf6ae1e9d.jpg?imageView2/2/w/4096/h/2048/q/100/format/jpg
有什麼問題?
沒有比賽,看到https://regex101.com/r/nX2rP5/1。檢查匹配是否爲空。嘗試'([^ \ /] +)\。\ w +(?:\?| $)'並訪問'[1]'項目。 –
@WiktorStribiżew你是對的!你可以發佈這個答案嗎? – alexchenco