【问题标题】:Return Value of Non-Whitespace Characters非空白字符的返回值
【发布时间】:2012-11-29 02:10:43
【问题描述】:

我正在尝试编写一个函数来返回文本区域内的非空白字符的值。我应该使用替换和给我的正则表达式来分隔空格和实际字符,但我只是不明白我应该如何获得计数。谁能给我解释一下?

function countText()
{
    var commentText;
    var commentBox = document.getElementById('comment');  //comment is my textArea ID
    var commentregx = "/\s/g";                            // whitespace regex

    commentText = commentBox.value.replace(commentregx, ""); // commentText is supposed to hold the number of non-whitespace values.
}

如果需要,我可以将其与其他功能一起放入 jsfiddle。

【问题讨论】:

    标签: javascript regex replace textarea


    【解决方案1】:

    replace 正在返回没有空格的字符串,所以只需抓住它的长度:

    commentText = commentBox.value.replace(commentregx, "").length;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-23
      • 1970-01-01
      • 2023-03-07
      相关资源
      最近更新 更多