Delete first character of a string in Javascript

前端 未结 14 1520
名媛妹妹
名媛妹妹 2020-11-28 01:01

I want to delete the first character of a string, if the first character is a 0. The 0 can be there more than once.

Is there a simple function that checks the first

14条回答
  •  醉梦人生
    2020-11-28 01:30

    The easiest way to strip all leading 0s is:

    var s = "00test";
    s = s.replace(/^0+/, "");
    

    If just stripping a single leading 0 character, as the question implies, you could use

    s = s.replace(/^0/, "");
    

提交回复
热议问题