javascript RegExp

Posted on 2011-05-09
Last Modified: 2012-05-11
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

Question by:oggiemc
    1 Comment
    LVL 75

    Accepted Solution

    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

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
    This article will show, step by step, how to integrate R code into a R Sweave document
    The viewer will learn how to count occurrences of each item in an array.
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now