Connecting Tech Pros Worldwide Help | Site Map

Regular Expression: match up to first colon in line

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 23rd, 2005, 07:06 PM
aliensite
Guest
 
Posts: n/a
Default 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:ratio<br />Second: 2:3<br>Third: size";
var output = html.match(regEx);
document.write(output);
</script>

Thanks for your help.
Dave


  #2  
Old July 23rd, 2005, 07:07 PM
Lasse Reichstein Nielsen
Guest
 
Posts: n/a
Default 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.'
  #3  
Old July 23rd, 2005, 07:07 PM
aliensite
Guest
 
Posts: n/a
Default 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

  #4  
Old July 23rd, 2005, 07:07 PM
RobB
Guest
 
Posts: n/a
Default 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>

  #5  
Old July 23rd, 2005, 07:10 PM
aliensite
Guest
 
Posts: n/a
Default 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 :)

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.