BeryJu
BeryJu

Reputation: 355

JavaScript Regex not detecting

I attempted to create a Regex that matches Opening HTML Tags.

<\w+((\s+\w+(\s*=\s*(?:\".*?\"|'.*?'|[^'\">\s]+))?)+\s*|\s*)>

Is what I have come up with. It works great in RegexPal.com http://gyazo.com/cef34f653c4a3483a31394330455c0cf But as soon as I try to use it on some text in JS (both Chrome and Node) this happens: http://gyazo.com/0c938ee289c1632f3f576aaccda1f81e

Rules is defined like that:

var Rules = [
    new RegExp("<\w+((\s+\w+(\s*=\s*(?:\".*?\"|'.*?'|[^'\">\s]+))?)+\s*|\s*)/>"),
    new RegExp("<\w+((\s+\w+(\s*=\s*(?:\".*?\"|'.*?'|[^'\">\s]+))?)+\s*|\s*)>"),
    new RegExp("</\w+((\s+\w+(\s*=\s*(?:\".*?\"|'.*?'|[^'\">\s]+))?)+\s*|\s*)>")
];

and Content is defined like that:

var Content = "<!DOCTYPE HTML><html><head><title>derp</title></head><body><div class=\"derp\"><!--this is formatted terribly -->derp<br /></div></body></html>";

Upvotes: 0

Views: 195

Answers (2)

epascarello
epascarello

Reputation: 207501

The problem is if you want to use RegExp() you need to double up the \.

 new RegExp("<\\w+((\\s+...

It would be better to drop the RegExp and just use /regExp/

var Rules = [
    /<\w+((\s+\w+(\s*=\s*(?:\".*?\"|'.*?'|[^'\">\s]+))?)+\s*|\s*)/>/,
    /<\w+((\s+\w+(\s*=\s*(?:\".*?\"|'.*?'|[^'\">\s]+))?)+\s*|\s*)>"/,
    /</\w+((\s+\w+(\s*=\s*(?:\".*?\"|'.*?'|[^'\">\s]+))?)+\s*|\s*)>/
];

Upvotes: 2

Paul Butcher
Paul Butcher

Reputation: 6956

When used inside a string, you need to escape \ to \\, otherwise you are just escaping whatever comes afterwards.

Either escape the \ characters or use the /regex/ syntax for defining a regex.

Upvotes: 0

Related Questions