user1893874 user1893874 - 3 months ago 9
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
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.

Comments