CSS2 Attribute Selectors with Regex
As for CSS 2.1, see http://www.w3.org/TR/CSS21/selector.html#attribute-selectors
Executive summary:
Attribute selectors may match in four ways:
[att]
Match when the element sets the "att" attribute, whatever the value of the attribute.
[att=val]
Match when the element's "att" attribute value is exactly "val".
[att~=val]
Match when the element's "att" attribute value is a space-separated list of
"words", one of which is exactly "val". If this selector is used, the words in the
value must not contain spaces (since they are separated by spaces).
[att|=val]
Match when the element's "att" attribute value is a hyphen-separated list of
"words", beginning with "val". The match always starts at the beginning of the
attribute value. This is primarily intended to allow language subcode matches
(e.g., the "lang" attribute in HTML) as described in RFC 3066 ([RFC3066]).
CSS3 also defines a list of selectors, but the compatibility varies hugely.
There's also a nifty test suite that that shows which selectors work in your browser.
As for your example,
a[href^=http]
{
background: url(external-uri);
padding-left: 12px;
}
should do the trick. Unfortunately, it is not supported by IE.
Regex to parse CSS selector
Thanks all very much for your suggestions and help. I tied it all together into the following two Regex Patterns:
This one parses the CSS selector string (e.g. div#myid.myclass[attr=1,fred=3]) http://www.rubular.com/r/2L0N5iWPEJ
cssSelector = re.compile(r'^(?P<type>[\*|\w|\-]+)?(?P<id>#[\w|\-]+)?(?P<classes>\.[\w|\-|\.]+)*(?P<data>\[.+\])*$')
>>> cssSelector.match("table#john.test.test2[hello]").groups()
('table', '#john', '.test.test2', '[hello]')
>>> cssSelector.match("table").groups()
('table', None, None, None)
>>> cssSelector.match("table#john").groups()
('table', '#john', None, None)
>>> cssSelector.match("table.test.test2[hello]").groups()
('table', None, '.test.test2', '[hello]')
>>> cssSelector.match("table#john.test.test2").groups()
('table', '#john', '.test.test2', None)
>>> cssSelector.match("*#john.test.test2[hello]").groups()
('*', '#john', '.test.test2', '[hello]')
>>> cssSelector.match("*").groups()
('*', None, None, None)
And this one does the attributes (e.g. [link,key~=value]) http://www.rubular.com/r/2L0N5iWPEJ:
attribSelector = re.compile(r'(?P<word>\w+)\s*(?P<operator>[^\w\,]{0,2})\s*(?P<value>\w+)?\s*[\,|\]]')
>>> a = attribSelector.findall("[link, ds9 != test, bsdfsdf]")
>>> for x in a: print x
('link', '', '')
('ds9', '!=', 'test')
('bsdfsdf', '', '')
A couple of things to note:
1) This parses attributes using comma delimitation (since I am not using strict CSS).
2) This requires patterns take the format: tag, id, classes, attributes
The first regex does tokens, so the whitespace and '>' separated parts of a selector string. This is because I wanted to use it to check against my own object graph :)
Thanks again!
Use Regex with selenium driver tag selectors
You can try using XPath contains:
List<WebElement> allElement = driver.FindElements(By.Xpath(".//<define root element here>[contains(@class, 'my-img-')]")
CSS difference between attribute selectors with tilde and star?
The asterisk attribute selector *=
matches any substring occurrence. You can think of it as a string contains call.
Input | Matches *=bar |
---|---|
foo | No |
foobar | Yes |
foo bar | Yes |
foo barbaz | Yes |
foo bar baz | Yes |
Select multiple div IDs using a single css selector
You can use attribute selectors (^=
is the starts with selector and $=
is the ends with selector):
$('div[id^="field_"][id$="_dd"]')
Although having this many unique IDs is pretty hard to manage. Give them a class as well.
Overriding contextual selectors
You can cheat with a negative margin-top:
.card {
border: solid 1px blue;
padding-top: 20px;
}
.card .card-heading {
border: solid 1px red;
margin-top: -20px;
}
http://jsfiddle.net/aemgb75b/
How to select a tag by just its first characters?
Well .getElementById()
is off the table, because you're working with the "name" attribute of your <iframe>
and not the "id". You can (in sufficiently modern browsers) use querySelector()
instead:
var frame = document.querySelector("[name^=tag_name_]");
The ^=
relation means, "starts with".
Looks like everything newer than IE8 should be OK with that.
Check string for valid CSS using Regex
Ok, so I came up with a regex that matches valid css styles..
([#.@]?[\w.:> ]+)[\s]{[\r\n]?([A-Za-z\- \r\n\t]+[:][\s]*[\w .\/()\-!]+;[\r\n]*(?:[A-Za-z\- \r\n\t]+[:][\s]*[\w .\/()\-!]+;[\r\n]*(?2)*)*)}
Working example here:
http://regex101.com/r/fK9mY3
Keep in mind.. After examining my example link above, you see this regex works for common, basic CSS2 styling, however, to match ALL CSS3 styling variations, pertaining to http://www.w3.org/TR/css3-syntax/#detailed-grammar, this regex would need some tweaking... The above regex IS a starting point however..
Can I use a regular expression in querySelectorAll?
You can't really use a regular expression in a selector but CSS selectors are powerful enough for your need with a "starts with" syntax inspired by regexes.
You can use a substring matching attribute selectors : link[type^=service]
Reads "Nodes of type link
with an attribute type
starting with "service"
From the formal specification:
[att^=val]
Represents an element with the att attribute whose value begins with the prefix "val". If "val" is the empty string then the selector does not represent anything.
Working JSFiddle
Select multiple div IDs using a single css selector
You can use attribute selectors (^=
is the starts with selector and $=
is the ends with selector):
$('div[id^="field_"][id$="_dd"]')
Although having this many unique IDs is pretty hard to manage. Give them a class as well.
Related Topics
How to See the Print Media CSS in Firebug
Center Output (Plots) in the Notebook
Text Field Not Working in Safari
Multiple !Important Class Declarations and Precedence
Putting a Inset Box Shadow on an Image or Image Within a Div
What Browsers Support "!Important"
Rstudio 0.98.1028 Add Background Image Only to Title Slide
CSS to Change the Cursor Style of the Resize Button on a Textarea
How to Prevent Child's Content from Stretching Parent's Width
Can You Blur the Content Beneath/Behind a Div
CSS Floats, Change Order on Mobile Layout
CSS Box-Shadow on Three Sides of a Div
How to Add Vertical Line Between Two Divs
Anyway to Have a Label Respond to :Focus CSS