• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 242
  • Last Modified:

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

0
oggiemc
Asked:
oggiemc
1 Solution
 
Michel PlungjanIT 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

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now