简体   繁体   English

如果单词有空格,如何计算文本区域中的单词

[英]How to count words in a textarea if the word has a space

I have a text input and a text area. 我有一个文本输入和一个文本区域。 I want to count the number of times the text input value occurs in the text area. 我想计算文本输入值在文本区域中出现的次数。 The following code works fine when the text input value doesn't have any spaces before or after. 当文本输入值的前后没有空格时,以下代码可以正常工作。 But when it has spaces, I'm getting a null even if it occurs in the text area. 但是当它有空格时,即使它出现在文本区域,我也会得到一个null。 For example: 例如:

text input value: "dog"
text area value: "dog"
I get "1" in the console log.

However 然而

text input value: "dog_"
text area value: "dog_"
I get "null" in the console log.

How do I get it so if I type "dog_" in the text input and there is a "dog_" in the text area, the count will be "1" 如何获得它,因此,如果我在文本输入中键入“ dog_”并且在文本区域中有一个“ dog_”,则计数将为“ 1”

function testCount() {

var textVal = document.getElementById("textInput").value;
var textArea = document.getElementById("textArea").value;
var regex = new RegExp("\\b" + textVal + "\\b",'gi');
var count = textArea.match(regex).length;

console.log(count);

}

I've tried various RegExp but they didn't solve the problem. 我已经尝试过各种RegExp,但是他们没有解决问题。 Thanks. 谢谢。

 function testCount() { var textVal = document.getElementById("textInput").value; var textArea = document.getElementById("textArea").value; var regex = new RegExp("(^|\\\\b)" + textVal + "(\\\\b|\\\\W|$)",'gi'); // UPDATE: Added "\\\\W" var count = textArea.match(regex); // To stop function if input is empty if (!textVal) return; // To prevent no-match cases from generating error if (!count) count = 0; else count = count.length; console.log(count); } 
 <input id="textInput" type="text" oninput="testCount()"> <textarea id="textArea"></textarea> 

UPDATE UPDATE

@rockstar suggested an even better RegExp. @rockstar建议使用更好的RegExp。

new RegExp("(^|[^\\w ])(" + textVal + ")([^\\w ]|$)",'gi')

Unlike the previous implementation, this RegExp ensures perfect match (including trailing spaces). 与以前的实现不同,此RegExp可确保完美匹配(包括尾随空格)。

If you feel confused with the complicated symbols, feel free to look at a very simple reference here . 如果您对复杂的符号感到困惑,请随时在此处查看非常简单的参考。

UPDATE 2 更新2

@rockstar again suggested an even better RegExp. @rockstar再次建议使用更好的RegExp。

new RegExp("(^|[^\\w ])" + textVal + "(?=[^\\w ]|$)",'gi');

You are using Regex word boundaries \\b in your regex but word boundaries do not include preceding or proceeding trailing spaces. 您正在使用正则表达式字边界\\b在您的正则表达式,但单词边界不包括前诉讼尾随空格。

As stated in this stackoverflow post ; 本stackoverflow帖子中所述 ;

\\b is similar to [^a-zA-Z0-9_] ie \\b would check for anything except word \\b类似于[^a-zA-Z0-9_]\\b检查 word以外的任何内容

This post also shows an alternative to word boundaries for searching including preceding and proceeding trailing spaces. 此信息还示出了用于搜索包括前述程序尾随空格字边界的替代方案。

Try changing 尝试改变

var regex = new RegExp("\\b" + textVal + "\\b",'gi');

to

var regex = new RegExp("(?<=\s|^)" + textVal + "(?=\s|$)", 'gi');

or even 甚至

var regex = new RegExp("(?:^|\s|$)" + textVal + "(?:^|\s|$)", 'gi');

The third code snippet is not as strict as the second one, but may be a little more compatible if you have issues with the second snippet. 第三个代码段不如第二个代码段严格,但如果第二个代码段存在问题,则兼容性可能会稍强一些。


Here is a working example: 这是一个工作示例:

 function testCount() { var textVal = document.getElementById("textInput").value; var textArea = document.getElementById("textArea").value; var regex = new RegExp("(?<=\\s|^)" + textVal + "(?=\\s|$)", 'gi'); var match = textArea.match(regex); var count = match ? match.length : 0; console.log(count); } document.getElementById('btn').addEventListener("click", testCount); 
 <input type="text" id="textInput"> <button id="btn"> Count </button> <textarea id="textArea"></textarea> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM