You can do:
var result = Math.max.apply(Math, textarea.value.match(/\d+/g).map(Number));
Gap:
textarea.value.match(/\d+/g)
Gets an array of numbers as strings.
.map(Number)
Maps each array entry from a string to a number.
Math.max.apply
Calls Math.max with this as Math and as a mapped array as parameters.
Change I did not understand that you needed to be between brackets. To do this, you will need a capture group, and now it is a little more complicated.
var reg = /\[(\d+)\]/g, numberStrings = [ ], match; while((match = reg.exec(textarea.value)) !== null){ numberStrings.push(match[1]); } var result = Math.max.apply(Math, numberStrings.map(Number));
It's a little harder to get an array of strings with numbers.
Another option, without using a capture group:
var numbersInBrackets = textarea.value.match(/\[\d+\]/g); var numbers = numbersInBrackets.map(function(x) { return Number(x.substring(1, x.length - 1)); }); var result = Math.max.apply(Math, numbers);
source share