简体   繁体   中英

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?

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

Thanks, amazing, it worked for me... (inputfile.match(/abc/gi)).length == (inputfile.match(/xyz/gi)).length Both counts are matching when really there are same counts of 'abc' and 'xyz' - returning expression to TRUE. And, not matching when counts are not regular for 'abc' and 'xyz' in turns returning FALSE.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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