
使用正则表达式获取 html 标签之间的内容
你想要获取的部分 html 代码如下:
<img src="http://p9.pccoo.cn/webapp/20161230/2016123010375539992519_300_300.gif" /> <img src="http://p9.pccoo.cn/webapp/20161230/2016123010380668807050_300_300.gif" />
你可以使用正则表达式来提取这两个 <img> 标签之间的内容,如下:
var string2 = "唉算阿萨德阿萨德阿萨德阿萨德阿萨德阿萨德阿萨德撒旦撒旦说道阿萨德说道阿萨德阿萨德阿萨德阿萨德阿萨德阿萨德阿萨德撒旦阿萨德撒旦阿萨德撒旦阿萨德是阿萨德<img src=\"http://p9.pccoo.cn/webapp/20161230/2016123010375539992519_300_300.gif\" /><img src=\"http://p9.pccoo.cn/webapp/20161230/2016123010380668807050_300_301.gif\" />"; var string3 = string2.replace(/\"/g, ''); // 移除引号 var regex = /^[\s\S]*?@@##@@[\s\S]*?$/; // 匹配第一个 `<img>` 标签的 `src` 属性值 var firstSrc = regex.exec(string3)[1]; regex = /^[\s\S]*?@@##@@$/; // 匹配第二个 `<img>` 标签的 `src` 属性值 var secondSrc = regex.exec(string3)[1]; console.log(firstSrc); console.log(secondSrc);











