简体   繁体   English

使用 RegEx 提取 HTML 元素的属性

[英]Extracting the property of an HTML element using RegEx

I'm working on some image deferring and am looking for a way to remove the src element.我正在处理一些图像延迟,并且正在寻找一种删除 src 元素的方法。 I have a specific use case where I need to use regex so I can render the result server-side and stop the src from loading, until I tell it to in JS.我有一个特定的用例,我需要使用正则表达式,以便我可以在服务器端呈现结果并停止加载 src,直到我在 JS 中告诉它。

Basically I need a way to reliably replace just the src in an image tag.基本上我需要一种方法来可靠地替换图像标签中的src Not the value, but the actual property name.不是值,而是实际的属性名称。

As of right now, I was testing something along the lines of:截至目前,我正在测试以下内容:

<img[^>]+src\\s*=\\s*['"]([^'"]+)['"][^>]*> . <img[^>]+src\\s*=\\s*['"]([^'"]+)['"][^>]*>

Which given my example text, finds the whole string <img src="aaaa" /> .给出我的示例文本,找到整个字符串<img src="aaaa" /> I basically need it to only highlight the src .我基本上只需要它来突出src That way I can regex_replace on it to change src to data-deferred .这样我就可以 regex_replace 将src更改为data-deferred

Example Text示例文本

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis bibendum lorem bibendum lectus rhoncus molestie. <img src ="aaaa" /> Maecenas ipsum justo, fermentum nec lacus in, porta consectetur metus.

Here is my Regex101: https://regex101.com/r/RATGgw/1/这是我的 Regex101: https ://regex101.com/r/RATGgw/1/

Since I don't always have access to the HTML I'm going to have to parse, I can't just target src .因为我并不总是可以访问我将不得不解析的 HTML,所以我不能只定位src I need to make sure it's only src when found anywhere within an image tag.我需要确保在图像标签内的任何地方找到它时它只是src

Any idea how I could modify this to accomplish this?知道我如何修改它以实现这一目标吗?

Edit:编辑:

For clarification, the question, how can I use RegEx to be able to find only the src property within an image tag.为了澄清问题,我如何使用 RegEx 才能仅在图像标签中找到src属性。

So my found result would allow me to replace src="https://" with a custom data-attribute like data-deferred="https://" .所以我找到的结果将允许我用自定义数据属性替换src="https://" ,如data-deferred="https://"

You can replace the src property with a data-deferred property by using capturing groups (parentheses) in your regex like this:您可以通过在正则表达式中使用捕获组(括号)来将src属性替换为data-deferred属性,如下所示:

 // [ $1 ][$2 ][?= a lookahead (no capture) ][global - replace all] var regex = /(<img[^>]+)(src)(?=\\s*=\\s*['"][^'"]+['"][^>]*>)/g; var text = 'Lorem ipsum. <img alt="foo" src ="aaaa" title="bar" /> Maecenas metus.'; var result = text.replace(regex, '$1data-deferred'); console.log(result); // Lorem ipsum. <img alt="foo" data-deferred ="aaaa" title="bar" /> Maecenas metus.

You want to use jQuery for things like this:您想将 jQuery 用于这样的事情:

$('img[data-deferred]').each(function(i, img){
  $(img).attr('src', $(img).attr('data-deferred'))
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM