.NET Framework Developer Center > .NET Development Forums > Regular Expressions > How do I look for all the alert and confirm prompts in a given web page using Regular Expressions?
Ask a questionAsk a question
 

AnswerHow do I look for all the alert and confirm prompts in a given web page using Regular Expressions?

  • Friday, November 06, 2009 9:02 AMxs8899 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    I need to look for all the alert and confirm prompts i a given web page.

    How do I do that using Regular Expressions?

    Currently I am using

    alert\\(.*\\)|confirm\\(.*\\)

    It works fine if the alert and confirm are on different lines.
    However it will return only the alert prompt if they are on the same line.
    e.g. alert('Hello World');confirm('Yes?');

    What am I doing wrong? Please advice. Thanks!!

Answers

  • Friday, November 06, 2009 1:16 PMmkashif Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    in regular expressions the repetition operator are greedy, that means that they will continue searching even after they've found a match until the string is exhausted. so in your case
       alert\(.*\)   will match alert('Hello World');confirm('Yes?')
    so to make it lazy we can add a '?' after the repetition operator as
       alert\(.*?\)|confirm\(.*?\)

    hope this works.
    • Marked As Answer byxs8899 Saturday, November 07, 2009 1:09 AM
    •  

All Replies

  • Friday, November 06, 2009 1:16 PMmkashif Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    in regular expressions the repetition operator are greedy, that means that they will continue searching even after they've found a match until the string is exhausted. so in your case
       alert\(.*\)   will match alert('Hello World');confirm('Yes?')
    so to make it lazy we can add a '?' after the repetition operator as
       alert\(.*?\)|confirm\(.*?\)

    hope this works.
    • Marked As Answer byxs8899 Saturday, November 07, 2009 1:09 AM
    •  
  • Saturday, November 07, 2009 1:10 AMxs8899 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Thanks! It work beautifully1