通过javascript / jquery删除/截断前导零

时间:2011-11-26 05:15:04

标签: javascript jquery numbers zero

建议通过javascript,jquery从数字(任意字符串)中删除或截断前导零的解决方案。

15 个答案:

答案 0 :(得分:147)

您可以使用在字符串开头匹配零的正则表达式:

s = s.replace(/^0+/, '');

答案 1 :(得分:22)

我会使用Number()函数:

var str = "00001";
str = Number(str).toString();
>> "1"

或者我会将我的字符串乘以1

var str = "00000000002346301625363";
str = (str * 1).toString();
>> "2346301625363"

答案 2 :(得分:11)

既然你说过“任何字符串”,我假设这是你想要处理的字符串。

"00012  34 0000432    0035"

所以,正则表达式是要走的路:

var trimmed = s.replace(/\b0+/g, "");

这样可以防止丢失“000000”值。

var trimmed = s.replace(/\b(0(?!\b))+/g, "")

您可以看到working example here

答案 3 :(得分:11)

也许有点晚了,但我想加2美分。

如果你的字符串ALWAYS代表一个带有可能的前导零的数字,你可以使用'+'运算符简单地将字符串转换为数字。

e.g。

x= "00005";
alert(typeof x); //"string"
alert(x);// "00005"

x = +x ; //or x= +"00005"; //do NOT confuse with x+=x, which will only concatenate the value
alert(typeof x); //number , voila!
alert(x); // 5 (as number)

如果你的字符串不代表一个数字而你只需要删除0就使用其他解决方案,但如果你只需要它们作为数字,这是最短的方法。

和FYI你可以做相反的事情,强制数字作为字符串,如果你连接一个空字符串,如:

x = 5;
alert(typeof x); //number
x = x+"";
alert(typeof x); //string

希望它可以帮助某人

答案 4 :(得分:3)

我在javascript中找到了截断前导零(数字或任何字符串)的解决方案:

<script language="JavaScript" type="text/javascript">
<!--
function trimNumber(s) {
  while (s.substr(0,1) == '0' && s.length>1) { s = s.substr(1,9999); }
  return s;
}

var s1 = '00123';
var s2 = '000assa';
var s3 = 'assa34300';
var s4 = 'ssa';
var s5 = '121212000';

alert(s1 + '=' + trimNumber(s1));
alert(s2 + '=' + trimNumber(s2));
alert(s3 + '=' + trimNumber(s3));
alert(s4 + '=' + trimNumber(s4));
alert(s5 + '=' + trimNumber(s5));
// end hiding contents -->
</script>

答案 5 :(得分:3)

parseInt(value) or parseFloat(value)

这将很好地工作。

答案 6 :(得分:2)

试试这个,

   function ltrim(str, chars) {
        chars = chars || "\\s";
        return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
    }

    var str =ltrim("01545878","0");

更多here

答案 7 :(得分:1)

您应该使用“parseInt”函数的“radix”参数: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt?redirectlocale=en-US&redirectslug=JavaScript%2FReference%2FGlobal_Objects%2FparseInt

parseInt('015',10)=&gt; 15

如果你不使用它,一些javascript引擎可能会将它用作八进制 parseInt('015')=&gt; 0

答案 8 :(得分:1)

如果number是int use

"" + parseInt(str)

如果号码是浮动使用

"" + parseFloat(str)

答案 9 :(得分:1)

简单地尝试乘以一,如下所示:

  

“ 00123” * 1; //获取数字   
  “ 00123” * 1 +“”; //以字符串形式获取

答案 10 :(得分:0)

没有正则表达式的另一种方式:

function trimLeadingZerosSubstr(str) {
    var xLastChr = str.length - 1, xChrIdx = 0;
    while (str[xChrIdx] === "0" && xChrIdx < xLastChr) {
        xChrIdx++;
    }
    return xChrIdx > 0 ? str.substr(xChrIdx) : str;
}

使用短字符串会比正则表达式(jsperf

更快

答案 11 :(得分:0)

const input = '0093';
const match = input.match(/^(0+)(\d+)$/);
const result = match && match[2] || input;

答案 12 :(得分:0)

1. 最明确的就是使用 parseInt():

parseInt(number, 10)

2. 另一种方法是使用 + 一元运算符:

+number

3.你也可以走正则表达式路线,像这样:

number.replace(/^0+/, '')

答案 13 :(得分:0)

常量编号 = '0000007457841'; console.log(+number) //7457841;

OR number.replace(/^0+/, '')

答案 14 :(得分:0)

来自 Guffa 的正则表达式解决方案,但至少留下一个字符

"123".replace(/^0*(.+)/, '$1'); // 123
"012".replace(/^0*(.+)/, '$1'); // 12
"000".replace(/^0*(.+)/, '$1'); // 0