How to get the most in textarea?

I have a text box:

<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 largest number that is in [] . In this case, I need to get 3 . How can i do this?

+5
source share
4 answers

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); 
+8
source

Same idea as MinusFour solution. Uses jQuery, but can easily be done without it.

 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 work of JSFiddle .

+2
source

Use this function to find the largest [number] on any line:

 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 counts the largest amount in your text box every time you click a button.

This allows you to play with the text box and retest the function using other 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 script !

+2
source

You need to perform 2 actions:

 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)); 
+2
source

Source: https://habr.com/ru/post/1241091/


All Articles