473,666 Members | 2,115 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to Match Line Breaks using Regular Expression

46 New Member
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.
Expand|Select|Wrap|Line Numbers
  1. function executeJSCodes( html ){        
  2.               var regExp = new RegExp('<script language="javascript">(.*)<\/script>',"gi");
  3.               while( res = regExp.exec(html) ){
  4.         eval(res[1]);
  5.               }        
  6. }
So, please tell me how to match line breaks by using regular expression.

Regards,
Velhari
Jul 1 '08 #1
4 2579
gits
5,390 Recognized Expert Moderator Expert
hi ...

try a replace at first, like this:

Expand|Select|Wrap|Line Numbers
  1. html = html.replace(/(\n\r)+/, '');
just add this to your function as the first step.

kind regards
Jul 1 '08 #2
Velhari
46 New Member
hi ...

try a replace at first, like this:

Expand|Select|Wrap|Line Numbers
  1. html = html.replace(/(\n\r)+/, '');
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;

Expand|Select|Wrap|Line Numbers
  1. function executeJSCodes( html ){        
  2.              var regExp = new RegExp('<script language="javascript">([\r\n\b\s\t]*.*[\r\n\b\s]*)<\/script>',"mgi");        
  3.              while( res = regExp.exec(html) ){            
  4.     eval(res[1]);
  5.              }        
  6. }
Regards,
Velhari
Jul 1 '08 #3
hsriat
1,654 Recognized Expert Top Contributor
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
Jul 1 '08 #4
rnd me
427 Recognized Expert Contributor
gits was close:

try:
Expand|Select|Wrap|Line Numbers
  1.  html = html.replace(/\r?\n/g, "")
Jul 1 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

0
1934
by: Follower | last post by:
Hi, I am working on a function to return extracts from a text document with a specific phrase highlighted (i.e. display the context of the matched phrase). The requirements are: * Match should be case-insensitive, but extract should have case preserved.
1
3975
by: Venkat | last post by:
Hi, I am using match function of string to find if a character is there in a string. The function Match is working fine with all the other characters except when the searching character is "+". Here is the piece of code i am using var line1 = "Hell+O";
3
18747
by: Derek Stone | last post by:
In my continuing inability to completely understand regular expressions I have a new one for you. I'd like to capture a string "A" unless it is anywhere in between string "B" and string "C". Therefore some matches are: XYZAHIJ ABC
19
2149
by: Tom Deco | last post by:
Hi, I'm trying to use a regular expression to match a string containing a # (basically i'm looking for #include ...) I don't seem to manage to write a regular expression that matches this. My (probably to naive) approach is: p = re.compile(r'\b#include\b) I also tried p = re.compile(r'\b\#include\b) in a futile attempt to use a backslash as escape character before the #
2
7930
by: Christian Staffe | last post by:
Hi, I would like to check for a partial match between an input string and a regular expression using the Regex class in .NET. By partial match, I mean that the input string could not yet be complete but I want to know if a match is possible so far. For instance I want to design a text box to enter a date and validate the correctness of the date as the user types character. If the user enters 1953/12/23 it will match my regex of course...
1
7396
by: Laser Lu | last post by:
Hi, all, I'm now writing a program to compress JavaScript code. One puzzle is how to write a regular expression to find out and remove all the redundent blank spaces. However, those blank spaces that are in the comments should be kept intact. I've tried to write some Regexs, and I list them here for your information: regex = new Regex(@"/\**?\*/"); // pattern used to match a multiline comment block regex = new Regex(@"//*\n"); //...
38
15950
by: Steve Kirsch | last post by:
I need a simple function that can match the number of beginning and ending parenthesis in an expression. Here's a sample expression: ( ( "john" ) and ( "jane" ) and ( "joe" ) ) Does .NET have something built-in that can accomplish this, or do I have to write my own parser? I don't want to reinvent the wheel if possible.
1
1516
by: sreemathy2000 | last post by:
My requirement is to read/write a javascript file in a windows applications. this javascript file is used by my website.. function test { var a; //start dim obj ={'abc','bcd','cde','def'}; //end Some other code here }
14
4976
by: Andy B | last post by:
I need to create a regular expression that will match a 5 digit number, a space and then anything up to but not including the next closing html tag. Here is an example: <startTag>55555 any text</aClosingTag> I need a Regex that will get all of the text between the html tags above (the html tags are random and i do not know them before hand). The match string always starts with at least 5 digits.
0
8448
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8783
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8552
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8640
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7387
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6198
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5666
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4369
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2773
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.