Search the archives!
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Javascript] regexp help: matching selectors
- From: hassan at webtuitive.com (Hassan Schroeder)
- Subject: [Javascript] regexp help: matching selectors
- Date: Wed Jun 29 18:47:52 2005
Paul Novitski wrote:
> I'm developing regular expressions that will match CSS-style selectors.
>
> Here's my first stumbler:
>
> /html( \w+)* div/
>
> (Intended to mean: the word "html" followed by zero-or-many instances of
> [a space followed by one-or-many word characters] followed by a space
> and then the word "div")
>
> I want this to match with:
>
> html div
> html body div
> html body div#logo
>
> etc. Can you explain to me why it's not working?
Not sure that I'm seeing the same problem.
In this quick test case, the first string passes, the second fails,
because it has two spaces between html and div, and your regexp only
allows one, *unless* there's also one or more word characters between
the spaces. Maybe /html(\s+\w+)*\s+div/ would be better...
<script type="text/javascript">
var sampleStrings = new Array(
"html div",
"html div",
"html body div",
"html #special div",
"html body#special div",
"html body.section div",
"html body div#logo",
"div p");
var checker = new RegExp(/html( \w+)* div/);
for ( i = 0; i < sampleStrings.length; i++ )
{
var thing = sampleStrings[i];
alert( (checker.test(thing))? thing+" is good" : "BAD thing "+thing);
}
</script>
.. or, if it's possible the middle element might have (or just be) a
CLASS or ID as in the samples above,
/html[\s+\.\#\w]*\s+div/i
would cover that, too.
--
Hassan Schroeder ----------------------------- hassan@xxxxxxxxxxxxxx
Webtuitive Design === (+1) 408-938-0567 === http://webtuitive.com
dream. code.
- References:
- [Javascript] regexp help: matching selectors
- From: Paul Novitski
- [Javascript] regexp help: matching selectors
- Prev by Date: [Javascript] regexp help: matching selectors
- Next by Date: [Javascript] how to work around this?
- Previous by thread: [Javascript] regexp help: matching selectors
- Next by thread: [Javascript] Internet Explorer, iFrames and JavaScript
- Index(es):