0

With the following input:

#element{ color: #333; }
#element2,
.element3,
.whatever{ background: red; }

.test,
.test2, .test3,
#test111,
.element5555, #fooooooter{ display: none; }

#element8 tr .bacon{ font-size: 10000em; }

How do I use regex to match these:

#element  
#element2  
#element8
#test111

And not match these:

#333

I tried more variations so far and I can't come up with anything useful.

This is what I tried the latest:

(#).*[{]

But it seems to skip some selectors.

Frantisek
  • 7,485
  • 15
  • 59
  • 102
  • 1
    This is non-trivial for a few reasons. There's a lot of ambiguity between words and color identifiers. The following are all valid colors: `#abc`, `#beefee`, `#a12`, `#beef12`. Differentiating those from IDs won't be 100% perfect. You could ignore text between `{` braces `}`, but they can span multiple lines. You might be able to throw a `:` into the mix and get something good enough for your needs, but a perfect multi-line regex is non-trivial. Be wary of any simple-looking answers below, if you need something perfect. – Jim Stewart Sep 21 '13 at 23:00
  • How come nobody has posted [the Chtulhu link](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags) yet? Don't use regex to parse structured information. – tripleee Sep 22 '13 at 06:59
  • @tripleee all people answered this, don't like to parse these kind of dirty strings with regex patterns but they like to use their minds figuring out how to solve new problems come to mind, and also regex, apart from it's meaning (*regular expression*), is more than dirty to be used for just dirty things! – revo Sep 22 '13 at 07:39

5 Answers5

5

#-?[_a-zA-Z]+[_a-zA-Z0-9-]*(?=[^}]*\{)

It explicitly matches valid characters in CSS selector. Which characters are valid in CSS class names/selectors?

http://regexr.com?36e6v

Don't forget to remove comments and quotation like content: '{}' before applying this regex. Matching comments and quotations is possible with regex. Remove it using unrolling skill, separately. (I think unrolling is complicated and inefficient enough to rule the integration out.)

It does match color if {} blocks are nested as are in Sass, less

Community
  • 1
  • 1
Herrington Darkholme
  • 5,979
  • 1
  • 27
  • 43
0

OK,Check out this one,it exactly matches what you need;

^\#[a-z]+[0-9]* //(Multiline = true)

If you need to capture ID Name,use this;

^\#(?<IDName>[a-z]+[0-9]*)  //(Multiline = true)

This regex has a capture group named IDName which gives you name of the ID.

Demo here.

devavx
  • 1,035
  • 9
  • 22
0

Okay, I think I got it (http://regexr.com?36e5i):

#[^{,;]*(?:[{,\n]) will also match ids followed by other selectors (eg., #element8 tr .bacon)

#[^ {,;]*(?:[ {,\n]) will only match ids only (so you'd get the #element8 part from the example above, but not the rest)

kalley
  • 18,072
  • 2
  • 39
  • 36
0

try this /#[^1-9]+/g

var t=" #element ";
var p=t.search(/#[^1-9]+/g);
alert(p);

or /#[^1-9.@#$%^&*()!]+/g if want to remove more symbols other then numbers

Ek1234
  • 407
  • 3
  • 7
  • 17
0

Upadated based on a comment

This will work for you:

/(?![^{]+})(#\S+)\b/g

Check it here: http://regex101.com/r/rF9iR9

revo
  • 47,783
  • 14
  • 74
  • 117