user1893874 user1893874 - 1 year ago 86
Javascript Question

min value from a string contains number, character and space

Encountered a string, contains both number as well Character with space like

" 7, 4, B, 9"

I am trying to get minimum value out of it.

By converting into an array and using Min function we can get the value out of it.

When we are having string and space with it, how to do it in one go.

Please suggest me

Answer Source
let myStr = ' 7, 4, B, 9'
Math.min.apply( null, myStr.match(/\d+/g) )

First we create the array using match with a simple regex. Then we apply the Math.min function to each element of the array.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download