Help! On the problem of regular grouping in js.

  node.js, question
The function of the code is to detect whether an html element contains a certain class, but in the process of using regular, I just don't understand why grouping is needed, what is the function of grouping, and how to match it here. What happens if no grouping is added?
 I have tried, if there is no grouping, the match is not correct.

<!  DOCTYPE html>
 <html>
 <head>
 <meta charset="UTF-8">
 < title > test < /title >
 <script type="text/javascript">
 window.onload=function(){
 var box=document.getElementById('box');
 
 //Check whether the class exists
 function hasClass(element,cName){
 alert(!  !  Element.classname.match (newregexp ('(\ \ s |)' plus cName' (\\s|$')'))))));
 bracket
 //call
 hasClass(box,'bb');  //false
 hasClass(box,'bbb');  //true
 bracket
 </script>
 </head>
 <body>
 < divid = "box" class = "bbAAA" > test < /div >
 </body>
 </html>

图片描述

function hasClass(element, cName) {
 Return ('plus element.className plus''). indexOf ('plus cName plus'') > -1;
 bracket

It is recommended to write this way, compatible with ie6 plus


Without grouping, the generated regularity is S | bbbS | $ which means matching S or BBBS or ending the three cases, unless your class is empty or only BBB can match.
Grouping, the generated regularity is (S |) bbb (S | $), which means matching space or beginning, then BBB, then space or end. These three parts constitute four situations.

Class=”bbb ccc “is matched by the combination of ^bbbs.
Class = “aabbb” is matched by the combination of sbbb$
Class=”aa bbb cc “is sbbbs matched.
Class=”bbb “is bbb $ match