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?

1 Answer 1

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]+');
Sign up to request clarification or add additional context in comments.

1 Comment

I can't use literal notation because I need a concatination of regex. As I found here: stackoverflow.com/questions/185510/… It's only possible with regexp object. So I see, I should use two backslashes instead of one. Gonna check it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.