2017-09-08 14 views
0

現在我有相匹配的圖像src屬性來獲得圖像的URL正則表達式,但我想正則表達式來獲得url只能做到第一白空間:比賽圖片src用正則表達式,直到空白 - 的Javascript

當前正則表達式: blog_details.match(/src=(.+)/)[1]: ""

利用當前的正則表達式,我得到:

"https://res.cloudinary.com/cot/image/upload/v1xxxx878705/cz3dbifffcb6ysto7cbp.jpg layout="responsive" width="600" height="auto"/>" 

如果不需要此部分:

layout="responsive" width="600" height="auto"/>" 
+0

沒有雙引號(「) –

回答

3

使用否定的字符類:JPG後

src=([^\s]+) 
+0

我必須在正則表達式後刪除'[1]:「」'' – Satyadev

+0

'[[ 1]'是指在你需要的結果數組中的第一個捕獲組 – revo

+0

我明白'^'匹配第一個輸入字符,但是'反斜槓'有什麼作用?我嘗試閱讀文檔但不知道 – Satyadev

2

這裏是一個正則表達式,將獲得一切,直到收盤報價或空白:

/src="(.*?)(?:"|\s)/ 

const 
 
    sourceUnclosed = '<img src="https://res.cloudinary.com/cot/image/upload/v1xxxx878705/cz3dbifffcb6ysto7cbp.jpg layout="responsive" width="600" height="auto"/>', 
 
    sourceClosed = '<img src="https://res.cloudinary.com/cot/image/upload/v1xxxx878705/cz3dbifffcb6ysto7cbp.jpg" layout="responsive" width="600" height="auto"/>', 
 
    regex = /src="(.*?)(?:"|\s)/; 
 
    
 
console.log(regex.exec(sourceUnclosed)[1]); 
 
console.log(regex.exec(sourceClosed)[1]);

但像@Anil說,它看起來像你的HTML缺少收盤報價爲src屬性。

+0

是'.jpg'丟失或我無法看到它的輸出段 – Satyadev

+1

我」?你可以在devtools日誌中看到這個URL – Thijs