I’m trying to find out if a string contains css code with this expression:
var pattern = new RegExp('\s(?[a-zA-Z-]+)\s[:]{1}\s*(?[a-zA-Z0-9\s.#]+)[;]{1}');
But I get “invalid regular expression” error on the line above…
What’s wrong with it?
found the regex here: http://www.catswhocode.com/blog/10-regular-expressions-for-efficient-web-development
It’s for PHP but it should work in javascript too, right?
That regular expression is very bad and I would avoid its source in the future. That said, I cleaned it up a bit and got the following result:
this matches something that looks like css, for example:
Here’s the fiddle to prove it, though I’d recommend to find a different solution to your problem. This is a very simple regex and it’s not save to say that it is reliable.