I’m getting this value being returned from the variable _text – 1 - 12 of 73 Items
The value being returned from the variable _text is always different.
I need to parse out the largest number (73).
Here’s what I’ve attempted:
var _text = $('#find-page .items-box:first > .items:first-child').html();
var nums = [];
$('_text').each( function() { nums.push( $(this).val() ); });
var max = Math.max.apply(Math, nums);
console.log(nums);
This is returning 0, which isn’t correct.
Any help would be greatly appreciated!
You can match all the numbers then map them and find the max:
var str = "1 - 12 of 73 Items";
var max = Math.max(...str.match(/\d+/g).map(Number));
console.log(max)
Answer:
If the string is the same every time, it is as simple as a regular expression matching the pattern
var str = " 1 - 12 of 73 Items"
console.log(str.match(/of\s(\d+)/)[1])
If it can be any numbers in a string
var str = " 123 456 890 90 33"
console.log(Math.max(...str.match(/(\d+)/g).map(x=>+x)))
Tags: exception, java, javascriptjavascript, jquery, string