I wanting to pass in a dynamic value for this regex but it's not working. I'm not sure what's missing from my code. I tried to escape the string but for some reason I missing something.
Any help is greatly appreciated.
var r = new RegExp('(?:^| )(myclass)(?: |$)'), m = (""+n.className).match(r); //found
var c = 'myclass';
var r = new RegExp("(?:^| )('\\b'" + c + "'\\b')(?: |$)"), m = (""+n.className).match(r); // not found
Edit --- when adding dynamic value (myclass) no node is returned. When it's hard coded I can find a node without issue.
var n = document.getElementById('parentID');
var c = 'myclass';
function find(n,c) {
do {
if (typeof n.className !== 'undefined') {
var r = new RegExp("(?:^| )(myclass)(?: |$)"), m = (""+n.className).match(r);
if (m !== null) {
n.setAttribute("id", "current");
}
}
if (n.hasChildNodes()) {
finds(n.firstChild)
}
}
while (n = n.nextSibling)
}