如何从JavaScript中包含HTML的字符串中提取background =值? [英] How do I extract a background= value from a string containing HTML in JavaScript?

查看:78
本文介绍了如何从JavaScript中包含HTML的字符串中提取background =值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含从另一个页面加载的HTML的字符串,如何使用Javascript从它的body标签中提取background属性?

I have a string containing HTML loaded from another page, how do I extract the background property from it's body tag using Javascript?

字符串中的body标签如下:

The body tag in the string looks like this:

<body onload='init();' background='storage/images/jsb_background.jpg' link='#000000' vlink='#000000' alink='#000000' leftmargin='0' topmargin='0' marginwidth='0' marginheight='0'>

谢谢!

推荐答案

我对此进行了正则表达式修补,它将在data字符串变量(包含HTML)中搜索bodybackground属性. >标签.正则表达式从此处被盗,并进行了一些修改.我对regex还是陌生的,所以我想它可以更流畅地完成,但仍然可以完成工作

I patched together a regex to do this, which will search the data string variable (containing the HTML) for the background attribute of the body tag. The regex is stolen from here and modified a bit. I'm still new to regex, so I guess it can be done more fluently, but it still gets the job done

var data = /* your html */;
var regex = /body.*background=["']?((?:.(?!["']?\s+(?:\S+)=|[>"']))+.)["']?/;
var result = regex.exec(data);
if (result.length > 1) {
    var background = result[1];
    alert(background);
}
else {
    //no match
}

这篇关于如何从JavaScript中包含HTML的字符串中提取background =值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆