javascript RegExp

Hello all,

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

window.onload=function() {
  var re = /(ds)+(j+s)/ig;
  var str = "cfdsJS *(&dsjjjsYJSjs 888dsdsJS";
  var resultArray = re.exec(str);
  while (resultArray) {
    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);

Open in new window

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

next match starts at 6
substring of ds
substring of JS

next match starts at 16
substring of ds
substring of jjjs

next match starts at 31
substring of ds
substring of JS

as shown here*%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
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.