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

Use Regex to form a number without leading zeros in Javascript - Stack Overflow

programmeradmin2浏览0评论

I'm having trouble forming a regular expression that can strips out leading zeros from numbers represented as strings. Sorry but parseFloat isn't what I'm looking for since I'll be dealing with numbers with 30+ decimal places.

My current regular expression is

/(?!-)?(0+)/;

Here are my test cases. /

$(function() {
    var r = function(val){
        var re = /(?!-)?(0+)/;
        return val.toString().replace( re, '');
    };
    test("positive", function() {
        equal( r("000.01"), "0.01" );
        equal( r("00.1"), "0.1" );
        equal( r("010.01"), "10.01" );
        equal( r("0010"), "10" );
        equal( r("0010.0"), "10.0" );
        equal( r("10010.0"), "10010.0" );
    });
    test("negative", function() {
        equal( r("-000.01"), "-0.01" );
        equal( r("-00.1"), "-0.1" );
        equal( r("-010.01"), "-10.01" );
        equal( r("-0010"), "-10" );
        equal( r("-0010.0"), "-10.0" );
        equal( r("-10010.0"), "-10010.0" );        
    });
});

Why are my test cases not passing?

I'm having trouble forming a regular expression that can strips out leading zeros from numbers represented as strings. Sorry but parseFloat isn't what I'm looking for since I'll be dealing with numbers with 30+ decimal places.

My current regular expression is

/(?!-)?(0+)/;

Here are my test cases. http://jsfiddle/j9mxd/1/

$(function() {
    var r = function(val){
        var re = /(?!-)?(0+)/;
        return val.toString().replace( re, '');
    };
    test("positive", function() {
        equal( r("000.01"), "0.01" );
        equal( r("00.1"), "0.1" );
        equal( r("010.01"), "10.01" );
        equal( r("0010"), "10" );
        equal( r("0010.0"), "10.0" );
        equal( r("10010.0"), "10010.0" );
    });
    test("negative", function() {
        equal( r("-000.01"), "-0.01" );
        equal( r("-00.1"), "-0.1" );
        equal( r("-010.01"), "-10.01" );
        equal( r("-0010"), "-10" );
        equal( r("-0010.0"), "-10.0" );
        equal( r("-10010.0"), "-10010.0" );        
    });
});

Why are my test cases not passing?

Share Improve this question edited May 16, 2012 at 21:00 Larry Battle asked May 16, 2012 at 20:37 Larry BattleLarry Battle 9,1885 gold badges43 silver badges55 bronze badges 2
  • Please provide some insight in what is actually going wrong, and asking an actual question may not hurt either. – Maarten Bodewes Commented May 16, 2012 at 20:44
  • The question is "why aren't my tests passing?" – Larry Battle Commented May 16, 2012 at 20:59
Add a ment  | 

3 Answers 3

Reset to default 3

This finishes all your cases

var re = /^(-)?0+(?=\d)/;
return val.toString().replace( re, '$1');

^ matches on the start of the string.

(-)? matches an optional - this will be reinserted in the replacement string.

(0+)(?=\d) matches a series of 0 with a digit following. The (?=\d) is a lookahead assertion, it does not match but ensure that a digit is following the leading zeros.

This passes your tests, and is relatively easy to read:

var r = function(val){
    var re = /^(-?)(0+)(0\.|[1-9])/;
    return val.toString().replace( re, '$1$3');
};

You could use the following:

var r = function(val) {
    var re = /(-)?0*(\d.*)/;
    var matches = val.toString().match(re);
    return (matches[1] || '') + matches[2];
};
发布评论

评论列表(0)

  1. 暂无评论