Regex with parseFloat

  • Thread starter Steffan A. Cline
  • Start date
S

Steffan A. Cline

I'm choking on figuring out the correct regexp to extract floats from a
string and then putting that into parseFloat().

parseFloat(somestring.replace(new RegExp("(\\s*\\s*)+?", "gi" ),"");

I got this hosed up. :D

Any suggestions?

Thanks,
Steffan
 
S

Steffan A. Cline

I'm choking on figuring out the correct regexp to extract floats from a
string and then putting that into parseFloat().

parseFloat(somestring.replace(new RegExp("(\\s*\\s*)+?", "gi" ),"");

I got this hosed up. :D

Any suggestions?

Thanks,
Steffan

var x= "$10.25";
x = x.replace(new RegExp("\D", "gi" ),"");
alert(x);


Always returns $10.25.

Ok, suggestions?
 
L

Lasse Reichstein Nielsen

Steffan A. Cline said:
I'm choking on figuring out the correct regexp to extract floats from a
string and then putting that into parseFloat().

parseFloat(somestring.replace(new RegExp("(\\s*\\s*)+?", "gi" ),"");

Extract floats or just one float?
Or remove everything non-floaty from the string?
Any suggestions?

Depends on which float formats you want to accept.
Before writing a regexp, it's always a good idea to describe, in words,
what it is going to match (and not match).

Let's say that floats are any sequence of digits, optionally containing
at most one decimal point, '.', which must not be at the end of the float.

Then a regexp to find that would be:
var float_re = /(\d*\.)?\d+/g;

You can extract the floats from a string by repeated application of exec:

var match;
while((match = float_re.exec(string))) {
alert("Float found: " + parseFloat(match[0]));
}

or all of them in one go, using match:

var floatStrings = string.match(float_re);
for (var i = 0; i < floatStrings.length; i++) {
alert("Float found: " + parseFloat(floatStrings[i));
}


/L
 
S

Steffan A. Cline

Steffan A. Cline said:
I'm choking on figuring out the correct regexp to extract floats from a
string and then putting that into parseFloat().

parseFloat(somestring.replace(new RegExp("(\\s*\\s*)+?", "gi" ),"");

Extract floats or just one float?
Or remove everything non-floaty from the string?
Any suggestions?

Depends on which float formats you want to accept.
Before writing a regexp, it's always a good idea to describe, in words,
what it is going to match (and not match).

Let's say that floats are any sequence of digits, optionally containing
at most one decimal point, '.', which must not be at the end of the float.

Then a regexp to find that would be:
var float_re = /(\d*\.)?\d+/g;

You can extract the floats from a string by repeated application of exec:

var match;
while((match = float_re.exec(string))) {
alert("Float found: " + parseFloat(match[0]));
}

or all of them in one go, using match:

var floatStrings = string.match(float_re);
for (var i = 0; i < floatStrings.length; i++) {
alert("Float found: " + parseFloat(floatStrings[i));
}


/L

These work great!

I just always need the first float found in the string. I was playing with
that. The idea is to always either get a 0 or the float value.

Thanks,

Steffan
 
T

Thomas 'PointedEars' Lahn

Steffan said:
I'm choking on figuring out the correct regexp to extract floats from a
string and then putting that into parseFloat().

parseFloat(somestring.replace(new RegExp("(\\s*\\s*)+?", "gi" ),"");

The second (or first) `\\s*' is redundant. Probably you wanted to match
something else.

With non-greedy matching (`+?') you will run into compatibility issues.

The `i' modifier is unnecessary as/if the expression does not contain letters.
I got this hosed up. :D

Yes.


PointedEars
 
L

Lasse Reichstein Nielsen

Steffan A. Cline said:
var x= "$10.25";
x = x.replace(new RegExp("\D", "gi" ),"");
alert(x);


Always returns $10.25.

Ok, suggestions?

Suggestion: "\\D".

/L
 
T

Thomas 'PointedEars' Lahn

kangax said:
Thomas said:
[...]
With non-greedy matching (`+?') you will run into compatibility issues.

Issues in browsers hardly relevant nowadays, don't you think? :)
Depends.

Looking at the archives:

Martin Honnen mentions that NN4 doesn't support non-greedy quantifiers
<http://groups.google.com/group/comp.lang.javascript/msg/34ded1154c4b88bf>

Correct, these features were introduced with JavaScript 1.5 (Netscape 6.0,
Mozilla 0.6 [or earlier?], Phoenix 0.1).
Jim Ley explains that non-greedy quantifies are not supported in JScript
until 5.5 (IE 5.01?)
<http://groups.google.com/group/comp.lang.javascript/msg/a1415f280544c4b9>

Until *before* JScript 5.5(.6330) (IE for Windows 5.5).
Are you aware of any others?

Not yet.
Btw, I don't see this incompatibility mentioned anywhere in es-matrix.

It is mentioned in the local version, and I have posted it (here?) before.
`String.prototype.trim` also appears to be missing.

Note the date. String.prototype.trim() was not introduced before JavaScript
1.8.1 (Firefox 3.5), released 2009-06-30 CE. The Matrix has you, though.


PointedEars
 
D

Dr J R Stockton

In comp.lang.javascript message said:
I'm choking on figuring out the correct regexp to extract floats from a
string and then putting that into parseFloat().

parseFloat(somestring.replace(new RegExp("(\\s*\\s*)+?", "gi" ),"");

To get the first float in a form suited to parseInt, assuming that there
is no need to reject floats that lack a decimal point or an exponent
part, it is only necessary to remove everything before the first decimal
digit, EXCEPT for an optional immediately preceding decimal point and an
optional sign immediately preceding that, which remain.

Numbers starting with a decimal point are despised.

A RegExp literal would be easier to use. This *might* be it :

somestring.replace(/^(\D*?)([-+]?)(\.)?(\d)/, "$2$3$4")

There seems no need for g it i.

For multiple floats, you should instead use .match, with a RegExp
literal that matches all permissible float patterns; and apply
parseFloat to the elements of the resulting array.
 

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
474,089
Messages
2,570,602
Members
47,223
Latest member
smithjens316

Latest Threads

Top