admin管理员组文章数量:1430924
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:
It's for PHP but it should work in javascript too, right?
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./blog/10-regular-expressions-for-efficient-web-development
It's for PHP but it should work in javascript too, right?
Share Improve this question asked Jan 29, 2012 at 17:12 AlexAlex 66.2k185 gold badges460 silver badges651 bronze badges 2- 4 I would suggest avoiding that website. Taking coding advice from a cat is probably a bad idea in general. – Pointy Commented Jan 29, 2012 at 17:32
- 1 While CSS isn't a terribly plex language, it is probably a bit too plex to be detected reliably with a single regex. My guess is that there are better ways to solve this problem. – benekastah Commented Jan 29, 2012 at 17:48
4 Answers
Reset to default 3What are the ?
at the start of the two [a-zA-z-]
blocks for? They look wrong to me.
The ?
is unfortunately somewhat overload in regexp syntax, it can have three different meanings that I know of, and none of them match what I see in your example.
Also, your \s
sequences need the backslash escaping because this is a string - they should look like \\s
. To avoid escaping, just use the /.../
syntax instead of new Regexp("...")
.
That said, even that is insufficient - the regexp still produces an Invalid Group
error in Chrome, probably related to the {1}
sequences.
The ?
's are messing it up. I'm not sure what they are for.
/\s[a-zA-Z\-]+\s*:\s*[a-zA-Z0-9\s.#]+;/
worked for me (as far as piling. I didn't test to see if it properly detected a CSS string).
Replace the quotes with /
(slashes):
var pattern = /\s([a-zA-Z-]+)\s[:]{1}\s*([a-zA-Z0-9\s.#]+)[;]{1}/;
You also don't need the new RegExp()
part either, which is why it's been removed; instead of using a quote or double quote to denote a string, JavaScript uses a slash /
to denote a regular expression, which isn't a normal string.
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:
var pattern = /\s(?:[a-zA-Z-]+)\s*:\s*(?:[^;\n\r]+);/;
this matches something that looks like css, for example:
background-color: red;
Here's the fiddle to prove it, though I'd remend 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.
本文标签: jqueryInvalid regular expression in javascriptStack Overflow
版权声明:本文标题:jquery - Invalid regular expression in javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745574792a2664263.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论