Infinite Possibilities
Infinite Possibilities

Reputation: 7466

Regular expression in javascript

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));

Upvotes: 0

Views: 113

Answers (1)

Nick Craver
Nick Craver

Reputation: 630389

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>/;

You can test both versions here.

Upvotes: 5

Related Questions