【发布时间】:2017-02-08 22:51:44
【问题描述】:
我正在研究一个正则表达式,如果它是真的,会进行一些字符串操作,但目前我的代码被频繁触发,所以我需要它更具体。正则表达式真的不是我的强项——我目前的正则表达式是
if(/(?!<a(.*?))<img src="(.*?[^<])" data-special="(t:).*"/.test(str)) {
// do this code
}
我的正则表达式需要找到具有特定属性值且未被 href 包围/包裹在链接中的 img 元素。例如,我希望它捕获以下内容:
<img src="http://example.com/image.jpg" data-special="t:https://www.twitter.com" />
替换成
<a href="http://example.com/"><img src="http://example.com/image.jpg" data-special="t:https://www.twitter.com" /></a>
所以if 语句只有在存在data-special 属性的字符串中存在img 标记并且包含t: 后跟任何链接时才会为真。感谢任何帮助!
【问题讨论】:
标签: javascript jquery regex