0

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)
}
2
  • it's an element = var n= document.getElementById('someDiv'); Commented Apr 9, 2014 at 11:48
  • thats okay but what is the value of n.classname? is it some string ? Commented Apr 9, 2014 at 12:07

3 Answers 3

1

try this :

var c = 'myclass';
var r = new RegExp("(?:^| )(\\b"+c+"\\b)(?: |$)");
console.log(r);

this will return the result as

RegExp /(?:^| )('\b'myclass'\b')(?: |$)/

suppose you dont want the \b on both sides of myclass then just use the code below :

var c = 'myclass';
var r = new RegExp("(?:^| )("+c+")(?: |$)");
console.log(r);

go ahead , fire your firebug, paste in it and hit Run

update

check this code its working:

var c = 'myclass';
var r = new RegExp("(?:^| )(\\b"+c+"\\b)(?: |$)");
console.log(r);

var str = "myclass";
var m = (""+str).match(r);
console.log(m);
Sign up to request clarification or add additional context in comments.

2 Comments

@user686483 what output are you expecting ?? can you give an example
Hi @aelor: I'm using this in a re-usable js function: 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) }
1

In the failing RegExp, you need to remove the single quotes around '\\b' I think.

EDIT: Fiddle at http://jsfiddle.net/UqL4J/ seems to suggest that your regexp with the single quotes removed does work as expected.

3 Comments

I did try this as well as many other combinations. It's something very simple I know but I just can't get the syntax right.
I just updated my answer with a fiddle that I hope is relevant.
Hi Peter, I've verified it's working but now my function is returning #text as a node as apposed to <div class="myclass"> </div> which is what it was returning with the hardcoded value originally. So two different issues here.
0

I don't believe that you need to be adding the characters before and after where you add the value into the regex ('\\b'). Try using the line

var r = new RegExp("(?:^| )(" + c + ")(?: |$)"), m = (""+n.className).match(r);

which will expand to

(?:^| )(myclass)(?: |$)

which is the same as the first RegEx line.

2 Comments

I tried this but it's failing for some reason. If I have "(?:^| )(" + c + ")(?: |$)" it fails but if I have "(?:^| )(myclass)(?: |$)" it's fine.
@user686483 If you extract the string from the constructor and print it, what is the output?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.