javascript RegExp

Hello all,

Can someone please explain whats going on in the attached code?
<script type="text/javascript">
//<![CDATA[

window.onload=function() {
  var re = /(ds)+(j+s)/ig;
  var str = "cfdsJS *(&dsjjjsYJSjs 888dsdsJS";
  var resultArray = re.exec(str);
  while (resultArray) {
    document.writeln(resultArray[0]);
    document.writeln(" next match starts at " + re.lastIndex + "<br />");
    for (var i = 1; i < resultArray.length; i++) {
      document.writeln("substring of " + resultArray[i] + "<br />");
    }
    document.writeln("<br />");
    resultArray = re.exec(str);
  }
}
//]]>
</script>
</head>
<body>
<p></p>
</body>
</html>

Open in new window

oggiemcAsked:
Who is Participating?
 
Michel PlungjanConnect With a Mentor IT ExpertCommented:
It is looking for

 1 or more ds plus 1 or more j  plus an s

in the string
"cfdsJS *(&dsjjjsYJSjs 888dsdsJS";

case insensitive - it will remember what is in the ()

which means

dsJS
next match starts at 6
substring of ds
substring of JS

dsjjjs
next match starts at 16
substring of ds
substring of jjjs

dsdsJS
next match starts at 31
substring of ds
substring of JS

as shown here

http://books.google.com/books?id=3On-moJDuO0C&pg=PA83&lpg=PA83&dq=cfdsJS+*%28%26dsjjjsYJSjs+888dsdsJS&source=bl&ots=pasqJus-Zq&sig=15zCrrgulVsixXlxu_hgZvQopVQ&hl=en&ei=zC7ITd2bF8HsOcOyme0B&sa=X&oi=book_result&ct=result&resnum=1&ved=0CBUQ6AEwAA#v=onepage&q&f=false
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.