Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
118 views
in Technique[技术] by (71.8m points)

Any suggestion on Javascript expression to count the number of occurrences of a particular string in Javascript

We are not native Java/Javascript programmers but sometimes get help from these languages to satisfy some of our job. We are working on some testing automation tool which supports XML-Javascript expression writing. There we have 'Verify' option to confirm if our required string is present in the input file or not. We usually use inputfile.indexOf('XYZ') expression to validate if XYZ string is present in the inputfile (this inputfile is just an array of characters from file, and is made readily available by tool to 'Verify' option. It contains some file data which we know, you can consider it as a random text file). Now, our requirement is to check if 'abc' is available and if it is available then 'xyz' should be also made available. Ultimately, the number of times 'abc' occurs in inputfile, same number of times 'xyz' also should be available in inputfile. In all these scenarios, my expression should result TRUE otherwise should always FALSE. Any suggestion on how to proceed for such expression in javascript ?

question from:https://stackoverflow.com/questions/65951540/any-suggestion-on-javascript-expression-to-count-the-number-of-occurrences-of-a

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use match and compare the lengths of the result. Something like:

const txt = `[**added demo abc] We are not native Java/Javascript programmers but sometimes get help from these languages to satisfy some of our job. We are working on some testing automation tool which supports XML-Javascript expression writing. There we have 'Verify' option to confirm if our required string is present in [**added demo abc] the input file or not. We usually use inputfile.indexOf('XYZ') expression to validate if XYZ string is present in the inputfile (this inputfile is just an array of characters from file, and is made readily available by tool to 'Verify' option. It contains some file data which we know, you can consider it as a random text file). Now, our requirement is to check if 'abc' is available and if it is available then 'xyz' should be also made available. Ultimately, the number of times 'abc' occurs in inputfile, same number of times 'xyz' also should be available in inputfile. In all these scenarios, my expression should result TRUE otherwise should always FALSE. Any suggestion on how to proceed for such expression in javascript?`;

const matchLen = (str, re) => (str.match(re) || {length: 0}).length;
console.log(`'abc' in txt and matches n of 'xyz' occurances in txt? ${
  matchLen(txt, /abc/gi) > 0 
    && matchLen(txt, /abc/gi) === matchLen(txt, /xyz/gi)}`);
  
console.log(`'expression' in txt and matches n of 'script' occurances in txt? ${
  matchLen(txt, /expression/gi) > 0 && 
    matchLen(txt, /expression/gi) === matchLen(txt, /script/gi)}`);
  
console.log(`'nothingmatcheshere' in text and matches n of 'script' occurances in txt? ${
  matchLen(txt, /nothingmatcheshere/gi) > 0 && 
    matchLen(txt, /nothingmatcheshere/gi) === matchLen(txt, /script/gi)}`);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...