I have a textarea like this:
<textarea>
this is a test [1] also this [2] is a test
and again [3] this is a test
</textarea>
Now I need to get the biggest number which is in []
. In this case I need to get 3
. How can I do that?
I have a textarea like this:
<textarea>
this is a test [1] also this [2] is a test
and again [3] this is a test
</textarea>
Now I need to get the biggest number which is in []
. In this case I need to get 3
. How can I do that?
- get the value of the textarea in a string variable, extract the values in [] using a regex, parse them to numbers and then sort them and get the highest value. – Roco CTZ Commented Jan 20, 2016 at 23:30
4 Answers
Reset to default 8You could do:
var result = Math.max.apply(Math, textarea.value.match(/\d+/g).map(Number));
Breaking it up:
textarea.value.match(/\d+/g)
Gets you an array of numbers as strings.
.map(Number)
Maps each entry of the array from a string to a number.
Math.max.apply
Calls Math.max
with this
as Math
and as parameters the mapped array.
Edit: I didn't realize what you needed had to be in between brackets. You'll need to use a capture group for that and it's a little bit more plicated now.
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 bit more tricky to get the array of strings with the numbers.
Another alternative, 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);
Same idea as MinusFour's solution. Uses jQuery but could easily be done without.
var content = $('textarea').val();
var contentArr = content.split(' ');
var nums = [];
for (var i = 0; i < contentArr.length; i++) {
var txt = contentArr[i];
if (txt.match(/[\d]/)) {
nums.push(Number(txt.slice(1,-1)));
}
}
// Max number is Math.max.apply(null, nums)
Full working JSFiddle.
You need to perform 2 actions:
- Get all the
[(numbers)]
with\[(\d+)]
regex - Get the max value from the resulting array (see this post)
Array.max = function( array ){
return Math.max.apply( Math, array );
};
var re = /\[(\d+)]/g;
var str = 'this is a test [1] also this [2] is a test\nand again [3] this is a test';
var numbers = []
while ((m = re.exec(str)) !== null) {
numbers.push(Number(m[1]));
}
document.write(Array.max(numbers));
Use this function to find the biggest [number]
in any string :
var biggestNumber = function(str) {
var pattern = /\[([0-9]+)\]/g, match, biggest = 0;
while ((match = pattern.exec(str)) !== null) {
if (match.index === pattern.lastIndex) {
pattern.lastIndex++;
}
match[1] = parseInt(match[1]);
if(biggest < match[1]) {
biggest = match[1];
}
}
return biggest;
}
DEMO
The following demo calculates the biggest number in your textarea every time you click the button.
It allows you to play around with the textarea and re-test the function with a different text.
var biggestNumber = function(str) {
var pattern = /\[([0-9]+)\]/g, match, biggest = 0;
while ((match = pattern.exec(str)) !== null) {
if (match.index === pattern.lastIndex) {
pattern.lastIndex++;
}
match[1] = parseInt(match[1]);
if(biggest < match[1]) {
biggest = match[1];
}
}
return biggest;
}
document.getElementById("myButton").addEventListener("click", function() {
alert(biggestNumber(document.getElementById("myTextArea").value));
});
<div>
<textarea rows="4" cols="50" id="myTextArea">
this is a test [1] also this [2] is a test
and again [3] this is a test
</textarea>
</div>
<div>
<button id="myButton">Try me</button>
</div>
See also this Fiddle!