1

I have to test classnames of different nodes in event delegation method I am using :

the test case I am using is

   function clickHandler(e){
        var evt = e || window.event;
        target = evt.target || evt.srcElement, 
        cls = target.className,id = target.id;

       if(/\bcell\b/i.test(cls)){
          //perform some action is node having classname cell is clicked 
       }
   }

but this particular test is match for nodes with classname .cell as well as .cell-validation-text.

The above test return true for both these nodes. How can i modify my regExp so as my above test returns true only for node having class .cell and false for .cell-validation text

2
  • Don't do it this way. Instead, put different handlers on different elements. Commented Jan 30, 2015 at 8:07
  • @torazaburo...sir...can you please illustrate this..? Commented Jan 30, 2015 at 8:14

2 Answers 2

3
/\bcell(\s|$)/

\s matches any whitespace character

$ matches the end of the string

EDIT: assuming you don't want to match "xxx-cell" either:

/(^|\s)cell(\s|$)/

^ matches the beginning of the string

Sign up to request clarification or add additional context in comments.

Comments

0

Just make use of anchors and alternation operator |

function clickHandler(e){
    var evt = e || window.event,
    target = evt.target || evt.srcElement, 
    cls = target.className,id = target.id;
    if(/\bcell(\s|$)/i.test(cls)){
       //perform some action is node having classname cell is clicked 
    }
}

But it would be better if you did something like

if(cls.split(" ").indexOf("cell") > -1){

}

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.