Extract a number from a string (JavaScript)

前端 未结 22 1431
灰色年华
灰色年华 2020-11-22 06:38

I have a string in JavaScript like #box2 and I just want the 2 from it.

I tried:

var thestring = $(this).attr(\'href\');
var          


        
22条回答
  •  再見小時候
    2020-11-22 07:20

    changeStrangeDate(dateString: string) {
    var sum = 0;
    var numbers = dateString.match(/\d+/g);
    if (numbers.length > 1) {
      numbers.forEach(element => {
        sum += parseInt(element);
      }
      );
    }
    console.log(new Date(sum).toDateString());
    return new Date(sum).toUTCString();
    

    }

    You can do it like that and then call function where you need, with parameter.

    this.changeStrangeDate('/Date(1551401820000-0100)/');
    

提交回复
热议问题