Skip to main content
Participant
June 20, 2007
Answered

regular expression

  • June 20, 2007
  • 2 replies
  • 403 views
Hello,

I need a little help with a regular expression. I would like to be able to find this string :

<!-- InstanceBeginEditable name="breadcrumb" -->

That is : a string beginning with "<!--" and containing as many white spaces as you wish, then "InstanceBeginEditable ", then as many white spaces as you wish, then "name=" ", then as many alphanumerical chars as you wish, then a quote, then as many white spaces as you wish and last "-->".

I'm trying to use this regular expression :

new RegExp("<!\-\-(\s)*InstanceBeginEditable(\s)*name=\"(\w)*\"(\s)*\-\->","g");

but as you can guess, it doesn't work.

A clue, anyone ?

TheGridOne
This topic has been closed for replies.
Correct answer
When you use the string form of the RegExp constructor you need to escape the escapes (ie \\ ) because the first one applies to string escaping so the regexp thinks you're looking for w* (or a lot of w's). You can also create your regexp using the more natural looking format, which also make it easier to read. Here's two examples, both should match the same thing:

var myRegExp1 = new RegExp("<!--(\\s)*InstanceBeginEditable(\\s)*name=\"(\\w)*\"(\\s)*-->","g");
var myRegExp2 = /<!--(\s)*InstanceBeginEditable(\s)*name="(\w)*"(\s)*-->/g;

Chris
Adobe Dreamweaver Engineering

2 replies

Participant
June 22, 2007
Hello,

Yes, you're right. It all works now !

Thanx a lot,

theGridOne
Correct answer
June 20, 2007
When you use the string form of the RegExp constructor you need to escape the escapes (ie \\ ) because the first one applies to string escaping so the regexp thinks you're looking for w* (or a lot of w's). You can also create your regexp using the more natural looking format, which also make it easier to read. Here's two examples, both should match the same thing:

var myRegExp1 = new RegExp("<!--(\\s)*InstanceBeginEditable(\\s)*name=\"(\\w)*\"(\\s)*-->","g");
var myRegExp2 = /<!--(\s)*InstanceBeginEditable(\s)*name="(\w)*"(\s)*-->/g;

Chris
Adobe Dreamweaver Engineering