0

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>";
4
  • 3
    You know what they say - if you've got one problem and try to solve it with RegEx, now you've got two problems. Commented May 17, 2013 at 14:51
  • 2
    Using reg exp to match html is a bad bad bad idea. Commented May 17, 2013 at 14:51
  • Well I was just playing around a little with the HTML syntax and tried to create a Tokenizer. Commented May 17, 2013 at 14:53
  • you can use DOM to parse it.. Commented May 17, 2013 at 15:05

2 Answers 2

2

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

Comments

0

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.

Comments

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.