
使用正则表达式获取 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);
以上就是如何使用正则表达式提取 HTML 标签之间的内容?的详细内容,更多请关注php中文网其它相关文章!
HTML怎么学习?HTML怎么入门?HTML在哪学?HTML怎么学才快?不用担心,这里为大家提供了HTML速学教程(入门课程),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号