【问题标题】:How to get biggest number in textarea?如何在textarea中获得最大的数字?
【发布时间】:2016-04-27 00:59:50
【问题描述】:

我有一个这样的文本区域:

<textarea>
this is a test [1] also this [2] is a test
and again [3] this is a test
</textarea>

现在我需要获取[] 中的最大数字。在这种情况下,我需要得到3。我该怎么做?

【问题讨论】:

  • 获取字符串变量中textarea的值,使用正则表达式提取[]中的值,将它们解析为数字,然后对它们进行排序,得到最大值。

标签: javascript jquery regex


【解决方案1】:

你可以这样做:

var result = Math.max.apply(Math, textarea.value.match(/\d+/g).map(Number));

分手:

textarea.value.match(/\d+/g)

为您获取一个字符串形式的数字数组。

.map(Number)

将数组的每个条目从字符串映射到数字。

Math.max.apply

使用this 调用Math.max 作为Math 并作为映射数组的参数。

编辑:我没有意识到你需要的东西必须放在括号之间。您需要为此使用捕获组,现在它有点复杂。

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));

获取带有数字的字符串数组有点棘手。

另一种选择,不使用捕获组:

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);

【讨论】:

  • 很好的解释......远远超出“试试这个” +1
  • 这种方法的一个问题是它无法获取[...] 中的数字。如果他们在场,它将考虑所有其他人。总体思路很棒,只需要多几行即可获得正确的数字。
  • 我明白了,我会为此添加一个注释。
  • @MinusFour 谢谢...等待您的编辑...我认为您必须编辑您的正则表达式...
  • .map(Number) 不是必需的。根据spec...在每个参数上调用 ToNumber
【解决方案2】:

与 MinusFour 的解决方案相同。使用 jQuery,但不用 jQuery 也可以轻松完成。

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)

完整的工作JSFiddle

【讨论】:

    【解决方案3】:

    使用这个函数在任意字符串中找到最大的[number]

    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;
    }
    

    演示

    以下演示会在您每次单击按钮时计算文本区域中的最大数字。

    它允许您使用 textarea 并使用不同的文本重新测试功能。

    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>

    另见this Fiddle

    【讨论】:

      【解决方案4】:

      您需要执行 2 个操作:

      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));

      【讨论】:

        猜你喜欢
        • 2015-08-16
        • 2016-02-22
        • 2018-04-29
        • 2019-06-06
        • 1970-01-01
        • 2011-02-10
        • 1970-01-01
        • 1970-01-01
        • 2018-06-23
        相关资源
        最近更新 更多