最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Regex issue - Replace floats, not int - Stack Overflow

programmeradmin3浏览0评论

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?

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?

Share Improve this question asked Apr 16, 2013 at 9:42 HarryHarry 13.3k30 gold badges112 silver badges169 bronze badges 3
  • 1 [^....] matches any individual character that isn't one of the ones inside the brackets. It's not looking for sequences. – Barmar Commented Apr 16, 2013 at 9:47
  • The matching bit works great, just not with the [] yes.. – Harry Commented Apr 16, 2013 at 9:48
  • I've corrected my answer using a negative look-around. Tested, works correctly my side. – Evan Knowles Commented Apr 16, 2013 at 9:50
Add a ment  | 

7 Answers 7

Reset to default 4

Try this instead

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

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

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

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

To replace:

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

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

var reg = /\d+\.\d+/g
var str = "1 south african rand is 0.11044";
var onlyFloats = str.match(reg).join(" ");
console.log(onlyFloats)

Try this

string = "1 south african rand is 0.11044"
reg = /(\d+\.\d+)/g;
string.match(reg);
/**
 * @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);
}

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]*))$
发布评论

评论列表(0)

  1. 暂无评论