0

I am counting how many times strings contain an aa appears in an array. I dont think there is something wrong with my code. like below

const regex_aa = /aa[^(aa)]?$/s;
let arr = [];
const sstr = ['aa', 'aaaa', 'cc', 'ccc', 'bbb', 'bbaa'];
sstr.filter(e => {
if (regex_aa.test(e)) {
  arr.push(e);
}
});

console.log(arr.length);

So the number is 3 which is correct. However, the next job is to count how many times appears, then it looks like

   const regex_aa = /aa[^(aa)]?$/s;
    const regex_bb = /bb[^(aa)]?$/s;
    let arr1 = [];
    let arr2 = [];

    const sstr = ['aa', 'aaaa', 'cc', 'ccc', 'bbb', 'bbaa'];
    sstr.filter(e => {
      if (regex_aa.test(e)) {
        arr1.push(e);
      }
      if (regex_bb.test(e)) {
        arr2.push(e);
      }
    });

    console.log(arr1.length, arr2.length);

so each time if I want to find the number of a new string, I have to create a new let, I found this way is a bit clumsy. Is there a better solution for counting strings? Thanks

9
  • 2
    If you use filter() you shouldn't push into an array, you should just return the comparison result. filter() will return the new array.
    – Barmar
    Sep 19, 2019 at 7:24
  • regex_js should be regex_aa. But your regex doesn't make much sense. If you just want to know if the string contains aa, it should just he /aa/
    – Barmar
    Sep 19, 2019 at 7:26
  • bb occurs twice in the sstr, so shouldn't its result be 2, not 1? Sep 19, 2019 at 7:26
  • [^(aa)]? doesn't mean whatever you think it does.
    – Barmar
    Sep 19, 2019 at 7:28
  • 1
    [^(aa)] means to match a single character that isn't (, a or ). It makes no sense to repeat a character inside [].
    – Barmar
    Sep 19, 2019 at 7:32

4 Answers 4

2

Using a regular expression here is overkill - instead, count up with .reduce by testing whether the string being iterated over .includes the substring you're looking for:

const countOccurrences = (arr, needle) => (
  arr.reduce((a, haystack) => a + haystack.includes(needle), 0)
);
console.log(countOccurrences(['aa', 'aaaa', 'cc', 'ccc', 'bbb', 'bbaa'], 'aa'));
console.log(countOccurrences(['aa', 'aaaa', 'cc', 'ccc', 'bbb', 'bbaa'], 'bb'));

0
1

It is better to use Array.reduce and make is as a function.

Also, there is not need to use regex in-order to find a substring inside a string, you can use String.indexOf for that

Something like this:

const sstr = ['aa', 'aaaa', 'cc', 'ccc', 'bbb', 'bbaa'];

function countAppearanceOf(needle, arr) {
  return arr.reduce((count, item) => count + (item.indexOf(needle) > -1 ? 1 : 0), 0);
}

console.log(countAppearanceOf('aa', sstr));

Or even more generelized method, you can create a predicate method.

const sstr = ['aa', 'aaaa', 'cc', 'ccc', 'bbb', 'bbaa'];

function generalCountAppearanceOf(needle, arr, predicate) {
  return arr.reduce((count, item) => count + (predicate(needle, item) ? 1 : 0), 0);
}

function generateCounterByPredicate(predicate) {
  return (needle, arr) => generalCountAppearanceOf(needle, arr, predicate);
}

function predicatWithIndexOf(needle, item) {
  return item.indexOf(needle) > -1;
}

function predicatWithRegex(needle, item) {
  return /bb(aa)+/.test(item);
}

const countAppearanceOfWithIndexOf = generateCounterByPredicate(predicatWithIndexOf);
const countAppearanceOfWithRegex = generateCounterByPredicate(predicatWithRegex);

console.log(countAppearanceOfWithIndexOf('aa', sstr));

console.log(countAppearanceOfWithRegex('aa', sstr));

0

You could just count up object properties

let word_count = { 
    aa: 0,
    bb: 0
 }

...

  if (regex_aa.test(e)) {
    word_count.aa += 1
  }
  if (regex_bb.test(e)) {
    word_count.bb += 1
  }

console.log(word_count) console.log(word_count.aa)

0
const v = 'aa';
new RegExp(v + '[^(' + v + ')]?$', 's')
2
  • You're making the same mistake he did with the [^(aa)] regexp, which makes no sense.
    – Barmar
    Sep 19, 2019 at 7:38
  • I've just wanted to give an option to user dynamic regexp, it doesnt matter what it is
    – tano
    Sep 19, 2019 at 7:41

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.