How to Find the Highest Number in a String Using JavaScript?

Estimated read time 1 min read

To find the highest number in a string using JavaScript, you can use regular expressions to extract all the numbers from the string and then use the Math.max method to find the highest number.

Here’s an example:

const string = "The highest number is 123 and the second highest is 45";

const numbers = string.match(/\d+/g).map(Number);
const highestNumber = Math.max(...numbers);

console.log(highestNumber); // Output: 123

In this example, the match method is used with a regular expression /\d+/g to extract all the numbers from the string "The highest number is 123 and the second highest is 45". The map method is used to convert the extracted strings to numbers. The spread operator (...) is used to pass the numbers as separate arguments to the Math.max method, which returns the highest number, which is 123 in this case.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply