开发者

Regular expression to match style="whatever:0; morestuff:1; otherstuff:3"

开发者 https://www.devze.com 2023-01-23 08:33 出处:网络
I\'m trying to match anything between and including style=\"\" eg: style=\"whatever:0; mor开发者_开发问答estuff:1; otherstuff:3\"The pattern will be /style=\"([^\"]*)\"/, but may vary a bit depending

I'm trying to match anything between and including style=""

eg: style="whatever:0; mor开发者_开发问答estuff:1; otherstuff:3"


The pattern will be /style="([^"]*)"/, but may vary a bit depending on what language you're using.

Also if you're trying to do this through javascript, jquery would make this as easy as

$("#element-id").attr("style");

If you're trying to do this from another language, use an HTML parsing lib as HTML isn't regular. BeautifulSoup for Python is quite nice.


String under test

style="whatever:0; morestuff:1; otherstuff:3"

Regex

style\s*=\s*"([^"]*)"

Contents of group 1

whatever:0; morestuff:1; otherstuff:3


Notice!

It is very hard to write a regex-based HTML parser that is correct, secure, and maintainable. If you need to write program that deals with HTML in a robust, reliable, and secure way, you should use a real HTML parsing library like jsoup (Java) or Html Agility Pack (C#). To find an HTML parser for your favorite language, Google: yourlanguage html parser.


If you need to remove all style tags from html (clean inline styles entirely), use this as regexp:

style=\"[^\"]*\"

This works for me in sublime text 2-3


/(style="([^"]*)")/

for the whole string (untested). do you want the key value pairs retrieved as well?

0

精彩评论

暂无评论...
验证码 换一张
取 消