regex - Javascript regular expression match and get -


i have string in javascript:

'nearly 2 pound/12345' 

and need check if string match pattern

'some string useless/number'  

and number.

i have created regex /\/\d+/g number,but char '/'.

so how can solve these?

if expression "forward-slash followed numbers", should suffice

let numbers = (/\/(\d+)/.exec(str) || []).pop() 

to break down...

  1. create regex forward-slash followed one-or-more digits (that want capture)

    let regex = /\/(\d+)/ 

    you may want / need tweak make more specific. example numbers appear @ end of string, use /\/(\d+)$/.

  2. execute regex against string. either return null no match or array entire match @ index 0 , capture group @ index 1, eg ["/12345", "12345]

    regex.exec(str) 
  3. create expression results in array, if no match made

    let arr = (regex.exec(str) || []) 
  4. use array.prototype.pop last array entry

    let numbers = arr.pop() 

numbers undefined if there no match.