Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the best way to remove non numeric characters from the beginning of a string?

In Javascript, what is the best way to remove non numeric characters from the beginning of a string?

-_1234d5fr

should ideally turn into

1234d5fr
like image 455
Mohammad Avatar asked Dec 23 '22 02:12

Mohammad


2 Answers

str = str.replace(/^\D+/, '');
  • regular-expressions.info/Character Classes, Anchors, and Repetition
    • \D stands for non-digit characters
    • The caret ^ matches the position before the first character in the string
    • + is "one or more of"
like image 137
polygenelubricants Avatar answered Dec 24 '22 16:12

polygenelubricants


How about...

str = str.replace(/^[^0-9]+/, '');
like image 32
VoteyDisciple Avatar answered Dec 24 '22 17:12

VoteyDisciple