简体   繁体   English

计算文本区域中的特定字符

[英]Counting specific characters in a textarea

Other questions have been asked concerning word count in text areas, but they are not duplicates of this question:已经提出了有关文本区域字数统计的其他问题,但它们不是这个问题的重复:

What I am trying to do is write a JavaScript/jQuery function is called on the user clicking on a button.我想要做的是编写一个 JavaScript/jQuery 函数,在用户单击按钮时调用它。 The function takes the value of a textarea and checks all the characters that are in there.该函数获取 textarea 的值并检查其中的所有字符。 And then check whether two characters have the same amount of occurrences.然后检查两个字符的出现次数是否相同。 In the function I need to use an if-else statement.在函数中,我需要使用 if-else 语句。

Example: If the input has 3x [ and 3x ] : if (a.length == b.length) {} ( = true )示例:如果输入有 3x [和 3x ]if (a.length == b.length) {} ( = true )

If the input has 3x [ and 2x ] : if (a.length == b.length) {} ( = false )如果输入有 3x [和 2x ]if (a.length == b.length) {} ( = false )

But I am not quite sure how I can count individual characters.但我不太确定如何计算单个字符。 I am guessing with some regex and a match , but the following does not seem to work:我在猜测一些正则表达式和match ,但以下似乎不起作用:

var a = str.match(/\[+ /g),
    b = str.match(/\[+ /g);

if (a.length == b.length) {
    //do something
}

Here is a fiddle .这是一个小提琴 As you can see, the count is not correct.如您所见,计数不正确。

How about:怎么样:

'a[b][ca[]de'.match(/\[/g).length;

output:输出:

3

as expected.正如预期的那样。

var str =  'What I am trying to do is write a JavaScript/jQuery function is called on the user clicking on a button. The function takes the value of a textarea and checks all the characters that are in there. And then check whether two characters have the same amount of occurrences. In the function I need to use an if-else statement.'

var matchFrequency = function(a,b, text) {
    return text.split(a).length == text.split(b).length
}
matchFrequency('a','a',str)
//true
matchFrequency('a','b',str)
//false

Edit编辑

var str = 'Hello [ Here are ] some random [ characters ]] And I need to [ count [ and ]'
matchFrequency('[',']',str)
//output - true

Would this work to you?这对你有用吗?

var aOccurrences = str.split("a").length - 1,
    bOccurrences = str.split("b").length - 1;

if(aOccurrences === bOccurrences) {
   //doSomething
}

DEMO演示

Make an array which is indexed with 'a' to 'z'.制作一个以“a”到“z”为索引的数组。

For example:例如:

var arr=[];

initialize the array with 0. I am giving code snippet here, I guess you can convert it into Javascript用 0 初始化数组。我在这里给出代码片段,我猜你可以把它转换成 Javascript

for(i=0; i<='z'-'a';i++) // 'z'-'a' means ASCII value of 'z' minus ASCII value of 'a'
arr[i] = 0;

string text_area = the text area from input

for(i =0; i<text_area.length ; i++) //text_area.length = length of your text area
if(text_area[i]>='a'&&text_area[i]<='z') // considering only lower case letters for now   
arr[text_area[i]-'a'] = arr[text_area[i]-'a'] + 1; //if a character is found,add 1 to count

We have our arr[] ready.我们已经准备好了 arr[]。 Now if we want to check if number of character 'C1' is equal to number of character 'C2', we need to check like this现在如果我们想检查字符 'C1' 的数量是否等于字符 'C2' 的数量,我们需要像这样检查

if(arr['C1'-'a']==arr['C2'-'a']) 

Hope this will help.希望这会有所帮助。

My answer is only partially, since I don't know how to handle regex under javascript/JQuery.我的回答只是部分,因为我不知道如何在 javascript/JQuery 下处理正则表达式。 I will write it in java.我会用java来写。 If you match each occurence of a and b, you can compare both :如果匹配 a 和 b 的每次出现,则可以比较两者:

Pattern pa=Pattern.compile("a");
Matcher ma=pa.match(str);
Pattern pb=Pattern.compile("b");
Matcher mb=pb.match(str);
int counta=0;
int countb=0;
while(ma.find())
counta++;
while(mb.find())
countb++;
boolean result=counta==countb;

This is an idea of the structure.这是结构的想法。 Hope it helps.希望能帮助到你。

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

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