0

I searched a lot ,and unable find to detect for loop that is inside my variable ;

I have a String like : "for(i=0;i<=1000;i++)" or it may be any kind of for loop !

I want to get first part of for loop : like for(i=0; and 1000 that how many times it run if digit is given.

I used for now that give me first part but it is not perfect :

 str.replace(/for\(\w+=\w;/g,"something");

it fails when i put some space in between!

Is it any way to get only the part of for loop Using regEx ??

aliteralmind
  • 19,847
  • 17
  • 77
  • 108
ashbuilds
  • 1,401
  • 16
  • 33
  • For loop statement for Javascript is specified [here](http://www.ecma-international.org/ecma-262/5.1/#sec-5.1.6) – P̲̳x͓L̳ Mar 09 '14 at 00:57
  • @P̲̳x͓L̳,I know what is for loop,But i want to detect it by using regEX,As i create a live editor so ,it is important me to check the usercode that is it not dangerous for my app ? So i only want to detect for loop using regEX. – ashbuilds Mar 09 '14 at 00:59
  • Out of curiosity: why do you want this? What are you parsing JavaScript with JavaScript? – Phrogz Mar 09 '14 at 01:29
  • Actully i creating a live editor,so if i ran the big loops so,it hangs my app, so i have to detect the loop if it is long then, alert the user that it is harmful. check out : http://stackoverflow.com/questions/21852811/how-to-prevent-loops-in-javascript-that-crash-the-browser-or-apps @Phrogz – ashbuilds Mar 09 '14 at 01:35
  • 1
    @AshishMishra If the editor does not need to interact with the DOM—if the user is writing JavaScript that is not interacting with the HTML page—you might be better served by [using a web worker](http://stackoverflow.com/questions/21956169/running-js-in-a-killable-thread-detecting-and-canceling-long-running-processe/21963123#21963123) so that you can kill any long-running code. What you have here works for a very, very small subset of code that could be written that would have problems. Infinite `while` loop, a `for` loop that doesn't increment, poor recursion, etc. – Phrogz Mar 10 '14 at 02:47

1 Answers1

3

This works:

\bfor\s*\([^;]+;.+\b(\d+)\b\s*;

Regular expression visualization

Debuggex Demo

This allows spaces between the for and open-paren, and also between the number in capture group one (the number you want) and the following semi-colon.

As far as how you "get" the number you want: It's in capture group one. I don't know what language you're using, but in Java, you'd retrieve it with something like:

Matcher m = Pattern.
   compile("\\bfor\\s*\\([^;]+;.+\\b(\\d+)\\b\\s*;").
   matcher(theSourceCode);

int theNumberYouWant = -1;
if(m.find())  {
   //Safe to translate to int, since the match guarantees it's a number
   theNumberYouWant = Integer.parseInt(m.group(1));
}

If you also need to capture the first number, before the first semicolon, just duplicate the current capture-group. So change this

\bfor\s*\([^;]+;.+\b(\d+)\b\s*;

to

\bfor\s*\(.+\b(\d+)\b\s*;.+\b(\d+)\b\s*;

and now the first number is in capture group 1, then second in capture group 2.

aliteralmind
  • 19,847
  • 17
  • 77
  • 108
  • Thanks for answer @aliteralmind, but How i get the number ? that is `i<=1000` ? – ashbuilds Mar 09 '14 at 01:05
  • It dosen't work when i give space after `for` like : `for (i=0;i<=1000;i++)` – ashbuilds Mar 09 '14 at 01:10
  • About to update it to allow for space between the for and open-paren. – aliteralmind Mar 09 '14 at 01:10
  • First time I see,someone in StackOverflow,who is so Kind !! :) thanks..@aliteralmind – ashbuilds Mar 09 '14 at 01:14
  • 1
    Updated my answer for your "how do I get it?" question. – aliteralmind Mar 09 '14 at 01:18
  • I also want to learn `regex` how i learn it like you ?:) @aliteralmind – ashbuilds Mar 09 '14 at 01:24
  • 1
    Consider using online tools like Debuggex, and I also highly recommend the Windows app RegexBuddy (http://www.regexbuddy.com). Here's a great question for more information: http://stackoverflow.com/questions/4736/learning-regular-expressions. Best of luck! – aliteralmind Mar 09 '14 at 01:41
  • I want to some change in your `regex`: I tried a lot but not able to do so. As i mention above i want to take : `for(i=0;` and another part is the number ,but your `regex` will provide me `for(i=0;i<=10`! please change it,i also try with `RegexBuddy` but confused !! :( @aliteralmind – ashbuilds Mar 09 '14 at 04:47
  • That sounds like a separate question, and I'd recommend asking a new one, so you can get some fresh help on it! :) – aliteralmind Mar 09 '14 at 04:48
  • I ask it above!for taking first part of it..another question means another `vote down` of mine... :( @aliteralmind – ashbuilds Mar 09 '14 at 04:51
  • I am waiting for your reply?? are you ? Or I have to make new one? :( @aliteralmind – ashbuilds Mar 09 '14 at 04:59
  • Assuming that I understand your question, which I'm not sure I do, I'm about to update my answer for it. After this, however, I'm going to bed. I'll check back again tomorrow. Consider posting a new question if you really have urgent needs that are beyond the scope of this original question. – aliteralmind Mar 09 '14 at 05:03
  • Thanku..again,please update..if it is not working right,then i'll make another question,but it is only for you :) @aliteralmind – ashbuilds Mar 09 '14 at 05:06