Search code examples

Regex using javascript to return just numbers

If I have a string like "something12" or "something102", how would I use a regex in javascript to return just the number parts?


  • Regular expressions:

    var numberPattern = /\d+/g;
    'something102asdfkj1948948'.match( numberPattern )

    This would return an Array with two elements inside, '102' and '1948948'. Operate as you wish. If it doesn't match any it will return null.

    To concatenate them:

    'something102asdfkj1948948'.match( numberPattern ).join('')

    Assuming you're not dealing with complex decimals, this should suffice I suppose.