简体   繁体   English

如果字符串为0,则从字符串中删除第一个字符

[英]Remove first character from a string if it is 0

I know it is related to Remove first character from a string if it is a comma but in this case that solution doesn't work for me. 我知道它与从字符串中删除第一个字符有关, 如果它是逗号但在这种情况下该解决方案对我不起作用。 Maybe its because now I'm dealing with a number. 也许是因为现在我正在处理一个数字。 Simply I want javascript to remove 0 from 08 but don't touch 10 ; 简单地说我希望javascript从08删除0但不要触摸10 ; only remove 0 when it is the first character. 只有当它是第一个字符时才删除0

This solution doesn't work: replace(/^0/, "") 此解决方案不起作用: replace(/^0/, "")

You can try this: 你可以试试这个:

alert("08".replace(/^0+/, ''));

DEMO DEMO

And if you are dealing with numbers then you can use parseInt() function. 如果你正在处理数字,那么你可以使用parseInt()函数。

Easy way with regex : 使用正则表达式的简单方法:

^0+(?!$) ^ 0 +(?!$)

and feel free to use String replaceFirst function. 并随意使用String replaceFirst函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM