Regex to get first number in string with other characters

前端 未结 10 1513
萌比男神i
萌比男神i 2020-11-30 11:34

I\'m new to regular expressions, and was wondering how I could get only the first number in a string like 100 2011-10-20 14:28:55. In this case, I\'d want it to

10条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-30 11:35

    /^[^\d]*(\d+)/
    

    This will start at the beginning, skip any non-digits, and match the first sequence of digits it finds

    EDIT: this Regex will match the first group of numbers, but, as pointed out in other answers, parseInt is a better solution if you know the number is at the beginning of the string

提交回复
热议问题