Delete first character of a string in Javascript

前端 未结 14 1533
名媛妹妹
名媛妹妹 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:41

    From the Javascript implementation of trim() > that removes and leading or ending spaces from strings. Here is an altered implementation of the answer for this question.

    var str = "0000one two three0000"; //TEST  
    str = str.replace(/^\s+|\s+$/g,'0'); //ANSWER
    

    Original implementation for this on JS

    string.trim():
    if (!String.prototype.trim) {
     String.prototype.trim = function() {
      return this.replace(/^\s+|\s+$/g,'');
     }
    }
    

提交回复
热议问题