473,799 Members | 3,209 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Regular Expression: match up to first colon in line

My code is too greedy, how can it be fixed?

Here is my code:

Desired output - First:,Second:, Third:
<br>
<script type="text/javascript">
var regEx = /[^<>]*?:/g;
var html = "<br>First:rati o<br />Second: 2:3<br>Third: size";
var output = html.match(regE x);
document.write( output);
</script>

Thanks for your help.
Dave

Jul 23 '05 #1
4 9175
"aliensite" <al*******@exci te.com> writes:
My code is too greedy, how can it be fixed?

Here is my code:

Desired output - First:,Second:, Third:
So, in words, you want the parts between a ">" and the following ":".
<script type="text/javascript">


var regEx = />([^:]*:)/g;
var html = "<br>First:rati o<br />Second: 2:3<br>Third: size";
var output = [];
while (match = regEx.exec(html )) {
output.push(mat ch[1]);
}
document.write( output);

Good luck
/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 23 '05 #2

Lasse Reichstein Nielsen wrote:
"aliensite" <al*******@exci te.com> writes:
My code is too greedy, how can it be fixed?

Here is my code:

Desired output - First:,Second:, Third:
So, in words, you want the parts between a ">" and the following ":".
<script type="text/javascript">


var regEx = />([^:]*:)/g;
var html = "<br>First:rati o<br />Second: 2:3<br>Third: size";
var output = [];
while (match = regEx.exec(html )) {
output.push(mat ch[1]);
}
document.write( output);

Good luck
/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors:

<URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html> 'Faith without judgement merely degrades the spirit divine.'


Thanks, I tried several expressions, but it seems only a loop works.
Dave

Jul 23 '05 #3
aliensite wrote:
Lasse Reichstein Nielsen wrote:
"aliensite" <al*******@exci te.com> writes:
My code is too greedy, how can it be fixed?

Here is my code:

Desired output - First:,Second:, Third:


So, in words, you want the parts between a ">" and the following ":".
<script type="text/javascript">


var regEx = />([^:]*:)/g;
var html = "<br>First:rati o<br />Second: 2:3<br>Third: size";
var output = [];
while (match = regEx.exec(html )) {
output.push(mat ch[1]);
}
document.write( output);

Good luck
/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors:

<URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
'Faith without judgement merely degrades the spirit divine.'


Thanks, I tried several expressions, but it seems only a loop works.
Dave


Possible alternative:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
</head>
<body>
<pre>
Input -
&amp;lt;br&amp; gt;First:ratio& amp;lt;br /&amp;gt;Seco nd:
2:3&amp;lt;br&a mp;gt;Third: size

Desired output -
First:,Second:, Third:

Actual output -
<script type="text/javascript">
var re = /<[^>]+>([^:]*):[^<]*/g;
var html = '<br>First:rati o<br />Second: 2:3<br>Third: size';
var output = html.replace(re , '$1:,').replace (/,$/, '');
document.writel n(output);
</script>
</pre>
</body>
</html>

Jul 23 '05 #4

RobB wrote:
aliensite wrote:
Lasse Reichstein Nielsen wrote:
"aliensite" <al*******@exci te.com> writes:

> My code is too greedy, how can it be fixed?
>
> Here is my code:
>
> Desired output - First:,Second:, Third:

So, in words, you want the parts between a ">" and the following ":".
> <script type="text/javascript">

var regEx = />([^:]*:)/g;
var html = "<br>First:rati o<br />Second: 2:3<br>Third: size";
var output = [];
while (match = regEx.exec(html )) {
output.push(mat ch[1]);
}
document.write( output);

Good luck
/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors:

<URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
'Faith without judgement merely degrades the spirit divine.'


Thanks, I tried several expressions, but it seems only a loop works. Dave


Possible alternative:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
</head>
<body>
<pre>
Input -
&amp;lt;br&amp; gt;First:ratio& amp;lt;br /&amp;gt;Seco nd:
2:3&amp;lt;br&a mp;gt;Third: size

Desired output -
First:,Second:, Third:

Actual output -
<script type="text/javascript">
var re = /<[^>]+>([^:]*):[^<]*/g;
var html = '<br>First:rati o<br />Second: 2:3<br>Third: size';
var output = html.replace(re , '$1:,').replace (/,$/, '');
document.writel n(output);
</script>
</pre>
</body>
</html>

Nice :)

Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
4187
by: Kenneth McDonald | last post by:
I'm working on the 0.8 release of my 'rex' module, and would appreciate feedback, suggestions, and criticism as I work towards finalizing the API and feature sets. rex is a module intended to make regular expressions easier to create and use (and in my experience as a regular expression user, it makes them MUCH easier to create and use.) I'm still working on formal documentation, and in any case, such documentation isn't necessarily the...
11
3922
by: Martin Robins | last post by:
I am trying to parse a string that is similar in form to an OLEDB connection string using regular expressions; in principle it is working, but certain character combinations in the string being parsed can completely wreck it. The string I am trying to parse is as follows: commandText=insert into (Text) values (@message + N': ' + @category);commandType=StoredProcedure; message=@message; category=@category I am looking to retrive name value...
3
2566
by: Zach | last post by:
Hello, Please forgive if this is not the most appropriate newsgroup for this question. Unfortunately I didn't find a newsgroup specific to regular expressions. I have the following regular expression. ^(.+?) uses (?!a spoon)\.$
25
5174
by: Mike | last post by:
I have a regular expression (^(.+)(?=\s*).*\1 ) that results in matches. I would like to get what the actual regular expression is. In other words, when I apply ^(.+)(?=\s*).*\1 to " HEART (CONDUCTION DEFECT) 37.33/2 HEART (CONDUCTION DEFECT) WITH CATHETER 37.34/2 " the expression is "HEART (CONDUCTION DEFECT)". How do I gain access to the expression (not the matches) at runtime? Thanks, Mike
0
10490
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10260
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
10243
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
10030
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
9078
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
7570
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
6809
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();...
1
4146
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
2
3762
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.