How to Match Line Breaks using Regular Expression

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Velhari
    New Member
    • Sep 2006
    • 46

    How to Match Line Breaks using Regular Expression

    Hi all,

    I wrote the following Javascript function used to execute the Javascript codes from the ajax response which contains both html & javascript.

    It works fine, if the javascript codes from the response doesn't have any line break at all.
    Code:
    function executeJSCodes( html ){		
                  var regExp = new RegExp('<script language="javascript">(.*)<\/script>',"gi");
                  while( res = regExp.exec(html) ){
    	    eval(res[1]);
                  }		
    }
    So, please tell me how to match line breaks by using regular expression.

    Regards,
    Velhari
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5390

    #2
    hi ...

    try a replace at first, like this:

    [CODE=javascript]html = html.replace(/(\n\r)+/, '');[/CODE]
    just add this to your function as the first step.

    kind regards

    Comment

    • Velhari
      New Member
      • Sep 2006
      • 46

      #3
      Originally posted by gits
      hi ...

      try a replace at first, like this:

      [CODE=javascript]html = html.replace(/(\n\r)+/, '');[/CODE]
      just add this to your function as the first step.

      kind regards
      hi,

      Thanks for Reply.

      I tried your code, it doen't work.

      From your regular expression instead of nullifying the line breaks, include this condition in my regular expression for matching line breaks, tab character, space at the beginning and end of string. It works fine now.

      Thanks for your valuable suggesstion

      For your info, modified code is below;

      Code:
      function executeJSCodes( html ){		
                   var regExp = new RegExp('<script language="javascript">([\r\n\b\s\t]*.*[\r\n\b\s]*)<\/script>',"mgi");		
                   while( res = regExp.exec(html) ){			
      	eval(res[1]);
                   }		
      }
      Regards,
      Velhari

      Comment

      • hsriat
        Recognized Expert Top Contributor
        • Jan 2008
        • 1653

        #4
        I could not understand the purpose of what you are trying to do, though I am able to guess that you are trying to locate the JavaScript inside the html and then execute it.

        This doesn't seem a right procedure to me. Please explain the whole thing so that we could think of an alternate solution, if you are willing to go for that.


        Regards

        Comment

        • rnd me
          Recognized Expert Contributor
          • Jun 2007
          • 427

          #5
          gits was close:

          try:
          Code:
           html = html.replace(/\r?\n/g, "")

          Comment

          Working...