1

I have a string in my js code, that actually is an html code. It's started with <li class='list__item item item_archive'> This works fine:

liMatches=s.match(/(<li[A-Za-z\s_=\']+>...$/);

where s is html string. Now I need to use RegExp object.

liMatchesRegex=new RegExp("<li[\s]+");

I create this object (I tried <li[A-Za-z\s_=\']+> too).

liMatchesConcat=s.match(liMatchesRegex);

shows me null It works only with liMatchesRegex=new RegExp("<li"); What's wrong?

Sergey Scopin
  • 2,217
  • 9
  • 39
  • 67

1 Answers1

1

You can just create the RegExp object using the literal notation:

var re = /<li[\s]+/;

Alternatively, if you want to use the constructor and pass in a string, you need to escape the backslashes (\\) in your expression:

var re = new RegExp('<li[\\s]+');
Robby Cornelissen
  • 91,784
  • 22
  • 134
  • 156
  • 2
    I can't use literal notation because I need a concatination of regex. As I found here: http://stackoverflow.com/questions/185510/how-can-i-concatenate-regex-literals-in-javascript It's only possible with regexp object. So I see, I should use two backslashes instead of one. Gonna check it. – Sergey Scopin Jul 28 '15 at 09:40