Forum Moderators: open

Message Too Old, No Replies

RegEx to extract css Class

         

kadnan

10:17 am on Sep 2, 2007 (gmt 0)

10+ Year Member



I am working on RegEx which would extract the content of css class. what I have done so far that i have matched the structure of css class. what have I done so far is given below:

[codes]
myClass\s*{[\s\w]*}
[/codes]

What it matches:

[codes]
myclass{
color
}
[/codes]

but what it doesn't match is:

[codes]
myclass{
color:red;
}
[/codes]

That is, it doesn't match : or ; etc.

How do I deal with it?

Thanks

Dabrowski

10:26 am on Sep 2, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



myClass\s*{[\s\w]*}

How much work have you done with RegExp's?

You're matching:

{ \s (whitespace) or \w (word character) * (many times) }

Why not just use:

myClass\s*{.*?}

Which will match everything between the {}'s, but the? will make sure it doesn't run through all the classes til the last one!

kadnan

2:50 pm on Sep 2, 2007 (gmt 0)

10+ Year Member


I am a newbie in RegEx.

Thanks I will try your code. Also I want to know whether Javascript support lookahead/lookbehind feature?

kadnan

5:26 am on Sep 3, 2007 (gmt 0)

10+ Year Member



your RegEx is not working.

Input:


myClass
{
d:r
}

Returns nothing

mehh

1:39 pm on Sep 3, 2007 (gmt 0)

10+ Year Member



try something like: myClass(.*?)}

Dabrowski

2:17 pm on Sep 3, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



kadnan, .* in a RegExp is pretty much infallible.

You're probably using the wrong function with the RegExp, there are a few that all do slightly different things, please post your JavaScript.

[edit] Also download this, it's invaluable: http://weitz.de/regex-coach/ [/edit]

[edited by: Dabrowski at 2:18 pm (utc) on Sep. 3, 2007]