I would like to match this regexp in javascript:
com\..*</div>
As you can see I want to have com. and then anything and then </div>.
But in javascript this is not working, it always founds the com/jdksf</div> not the com.fdsfd<div> text.
Any idea why is that?
Edit: My code looks like this:
var patt1=new RegExp("com\..*</div>");
alert(patt1.exec(document.getElementsByTagName("body")[0].innerHTML));
asked Nov 25, 2010 at 10:45
Infinite Possibilities
7,45613 gold badges60 silver badges124 bronze badges
-
1Post your actual code please - can't tell anything from such a short description.Benubird– Benubird2010年11月25日 10:47:18 +00:00Commented Nov 25, 2010 at 10:47
-
The code you included in your edit doesn't have any \escape characters at allGareth– Gareth2010年11月25日 10:53:08 +00:00Commented Nov 25, 2010 at 10:53
-
Added, I used this \escape, but it isn't workingInfinite Possibilities– Infinite Possibilities2010年11月25日 10:53:43 +00:00Commented Nov 25, 2010 at 10:53
-
you need a double escape because it's a string, see my answer below :)Nick Craver– Nick Craver2010年11月25日 10:54:36 +00:00Commented Nov 25, 2010 at 10:54
1 Answer 1
You need to escape the ., like this:
var patt1=new RegExp("com\\..*</div>");
The double backslash is because it's a string, so \\. is really \. in the regex. Or, declare it as a regex object directly:
var patt1 = /com\..*<\/div>/;
answered Nov 25, 2010 at 10:49
Nick Craver
631k138 gold badges1.3k silver badges1.2k bronze badges
Sign up to request clarification or add additional context in comments.
5 Comments
Benubird
won't solve the problem though - '.' matches anything, including the literal '.' character.
Nick Craver
@Benubird - this is escaping it, so it's
\. in the end, which is a literal match.Benubird
Right, what I meant was, if it's not matching com.foo with com..*, changing it to con\..* won't help - but I see I misread the question, your solution is correct in this case. +1
Infinite Possibilities
I think this will be ok, I just have some problem, hold on, I try to resolve it.
Infinite Possibilities
Does it matter if I have a line break before the
</div>. I don't think so.lang-js