1

Im want to remove all but the float in this sting:

string = "1 south african rand is 0.11044"

Im doing it like this:

reg = /[^\d+.\d+]/g
console.log string.replace(reg, '')

that logs

10.11044

that is wrong, I want only the xxxx.xxxxx part. 1 is not a float so it should not be part of this?

How should I chage it?

3
  • 1
    [^....] matches any individual character that isn't one of the ones inside the brackets. It's not looking for sequences. Commented Apr 16, 2013 at 9:47
  • The matching bit works great, just not with the [] yes.. Commented Apr 16, 2013 at 9:48
  • I've corrected my answer using a negative look-around. Tested, works correctly my side. Commented Apr 16, 2013 at 9:50

7 Answers 7

4

Try this instead

^((?!\d+\.\d+).)*

See this answer for more details: Regular expression to match a line that doesn't contain a word?

Sign up to request clarification or add additional context in comments.

Comments

2

Following regex should match floats for you (for the example provided):

/(-?\d*\.\d+)/

To replace:

console.log (string.replace(/(-?\d*\.\d+)/, ''));

2 Comments

He was trying to match words that were not floats however, to remove them.
I provided him regex to match float numbers, that can be used to replace. See my edited answer.
1

I have used positive look behind (?<=..) in below regular expression

\.\d+(?<=\d)

Use this regular expression and replace below value with ''. The result will be 1 23 33 3

1 23 33.2000 3.4445

Hope it helps

1 Comment

I think his desired result is 33.2000 3.4445. 1 and 23 should be excluded because they don't have a decimal point and fraction.
1
var reg = /\d+\.\d+/g
var str = "1 south african rand is 0.11044";
var onlyFloats = str.match(reg).join(" ");
console.log(onlyFloats)

Comments

0

Try this

string = "1 south african rand is 0.11044"
reg = /(\d+\.\d+)/g;
string.match(reg);

Comments

0
/**
 * @param $string
 * @return string
 * 
 * output
 * a.a.a.a.a.a.   => 0.0
 * 1.1.1.1.1.1.   => 1.11111
 * 2a$2.45&.wer.4 => 22.454 
 */
function stringToFlout($string) {
    $parts = explode('.', $string);

    for ($i = 0; $i < count($parts); $i++) {
        $parts[$i] = preg_replace('/[^0-9]/', '', $parts[$i]);
    }

    $left = $parts[0];
    if (empty($left))
        $left = 0;

    $float[] = $left;

    unset($parts[0]);
    $right = implode('', $parts);
    if (empty($right))
        $right = 0;

    $float[] = $right;

    return implode('.', $float);
}

1 Comment

Hello, and welcome to Stack Overflow. please add some explanation to your code - don't just answer with only code.
0

I would use this regex:

(0|([1-9][0-9]*))?\.(0|([1-9][0-9]*))

The (0|([1-9][0-9]*))? matches an optional integer part, the \. matches dot, and the final (0|([1-9][0-9]*)) matches a mandatory fractional part. It succeeds for:

  • .5
  • 0.5
  • 0.0
  • 0.1
  • 0.5foo
  • 31.41592

It fails for:

  • 00.5
  • 100
  • nice
  • 2.

Anchored version:

^(0|([1-9][0-9]*))?\.(0|([1-9][0-9]*))$

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.