Skip to content Skip to sidebar Skip to footer

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

Solution 1:

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/

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

Solution 2:

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.

Post a Comment for "Javascript Regex Not Detecting"