[英]match image src with regex until whitespace - Javascript
现在我有正则表达式,它匹配image src
属性以获取图像url,但是我想正则表达式仅在第一个空格之前获取url:
当前正则表达式: 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"/>"
使用否定的字符类:
src=([^\s]+)
这是一个正则表达式,它将获取所有内容,直到结束引号或空格为止:
/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
属性的右引号。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.