Connecting Tech Pros Worldwide Help | Site Map

Regular Expression: match up to first colon in line

aliensite
Guest
 
Posts: n/a
#1: Jul 23 '05
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:ratio<br />Second: 2:3<br>Third: size";
var output = html.match(regEx);
document.write(output);
</script>

Thanks for your help.
Dave

Lasse Reichstein Nielsen
Guest
 
Posts: n/a
#2: Jul 23 '05

re: Regular Expression: match up to first colon in line


"aliensite" <aliensite@excite.com> writes:
[color=blue]
> My code is too greedy, how can it be fixed?
>
> Here is my code:
>
> Desired output - First:,Second:,Third:[/color]

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

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

Good luck
/L
--
Lasse Reichstein Nielsen - lrn@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
aliensite
Guest
 
Posts: n/a
#3: Jul 23 '05

re: Regular Expression: match up to first colon in line



Lasse Reichstein Nielsen wrote:[color=blue]
> "aliensite" <aliensite@excite.com> writes:
>[color=green]
> > My code is too greedy, how can it be fixed?
> >
> > Here is my code:
> >
> > Desired output - First:,Second:,Third:[/color]
>
> So, in words, you want the parts between a ">" and the following ":".
>[color=green]
> > <script type="text/javascript">[/color]
>
> var regEx = />([^:]*:)/g;
> var html = "<br>First:ratio<br />Second: 2:3<br>Third: size";
> var output = [];
> while (match = regEx.exec(html)) {
> output.push(match[1]);
> }
> document.write(output);
>
> Good luck
> /L
> --
> Lasse Reichstein Nielsen - lrn@hotpop.com
> DHTML Death Colors:[/color]
<URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>[color=blue]
> 'Faith without judgement merely degrades the spirit divine.'[/color]

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

RobB
Guest
 
Posts: n/a
#4: Jul 23 '05

re: Regular Expression: match up to first colon in line


aliensite wrote:[color=blue]
> Lasse Reichstein Nielsen wrote:[color=green]
> > "aliensite" <aliensite@excite.com> writes:
> >[color=darkred]
> > > My code is too greedy, how can it be fixed?
> > >
> > > Here is my code:
> > >
> > > Desired output - First:,Second:,Third:[/color]
> >
> > So, in words, you want the parts between a ">" and the following[/color][/color]
":".[color=blue][color=green]
> >[color=darkred]
> > > <script type="text/javascript">[/color]
> >
> > var regEx = />([^:]*:)/g;
> > var html = "<br>First:ratio<br />Second: 2:3<br>Third: size";
> > var output = [];
> > while (match = regEx.exec(html)) {
> > output.push(match[1]);
> > }
> > document.write(output);
> >
> > Good luck
> > /L
> > --
> > Lasse Reichstein Nielsen - lrn@hotpop.com
> > DHTML Death Colors:[/color]
> <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>[color=green]
> > 'Faith without judgement merely degrades the spirit divine.'[/color]
>
> Thanks, I tried several expressions, but it seems only a loop works.
> Dave[/color]

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;Second:
2:3&amp;lt;br&amp;gt;Third: size

Desired output -
First:,Second:,Third:

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

aliensite
Guest
 
Posts: n/a
#5: Jul 23 '05

re: Regular Expression: match up to first colon in line



RobB wrote:[color=blue]
> aliensite wrote:[color=green]
> > Lasse Reichstein Nielsen wrote:[color=darkred]
> > > "aliensite" <aliensite@excite.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[/color][/color]
> ":".[color=green][color=darkred]
> > >
> > > > <script type="text/javascript">
> > >
> > > var regEx = />([^:]*:)/g;
> > > var html = "<br>First:ratio<br />Second: 2:3<br>Third: size";
> > > var output = [];
> > > while (match = regEx.exec(html)) {
> > > output.push(match[1]);
> > > }
> > > document.write(output);
> > >
> > > Good luck
> > > /L
> > > --
> > > Lasse Reichstein Nielsen - lrn@hotpop.com
> > > DHTML Death Colors:[/color]
> > <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>[color=darkred]
> > > 'Faith without judgement merely degrades the spirit divine.'[/color]
> >
> > Thanks, I tried several expressions, but it seems only a loop[/color][/color]
works.[color=blue][color=green]
> > Dave[/color]
>
> 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;Second:
> 2:3&amp;lt;br&amp;gt;Third: size
>
> Desired output -
> First:,Second:,Third:
>
> Actual output -
> <script type="text/javascript">
> var re = /<[^>]+>([^:]*):[^<]*/g;
> var html = '<br>First:ratio<br />Second: 2:3<br>Third: size';
> var output = html.replace(re, '$1:,').replace(/,$/, '');
> document.writeln(output);
> </script>
> </pre>
> </body>
> </html>[/color]


Nice :)

Closed Thread