Regular Expression: match up to first colon in line

A

aliensite

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
 
L

Lasse Reichstein Nielsen

aliensite said:
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: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
 
A

aliensite

Lasse said:
aliensite said:
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: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
'Faith without judgement merely degrades the spirit divine.'

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

RobB

aliensite said:
Lasse said:
aliensite said:
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: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
'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;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>
 
A

aliensite

RobB said:
aliensite said:
Lasse said:
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: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
'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;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>


Nice :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top