-2

I want to be able to specify a list of characters/strings and find out how many occurrences from the list are in a given string.

const match = ['&', '|', '-', 'abc'];
const input = 'This & That | Or | THIS - Foo abc';

result should return 5.

I can loop over the match and do indexOf with a counter, but I'm wondering if there is some easier way with reduce.

3
  • 1
    what effort have you made? Feb 14, 2017 at 1:21
  • are the match options only ever going to be a single character? is case important to the matching? (ie. T =/= t)
    – haxxxton
    Feb 14, 2017 at 1:26
  • no, it may be a string of characters for one match.
    – chovy
    Feb 14, 2017 at 6:01

4 Answers 4

2

Rather than looping over the match array, you could split the input string, filter it, and then get the length.

Here is a simple one-liner:

input.split('').filter(x => match.indexOf(x) > -1).length;

Snippet:

const match = ['&', '|', '-'];
const input = 'This & That | Or | THIS - Foo';
const count = input.split('').filter(x => match.indexOf(x) > -1).length;

console.log(count); // 4

0
1

You can use .map(), .filter(), with .reduce() chained to add element of resulting array

const match = ['&', '|', '-'];
const input = 'This, & That | Or | THIS - Foo';

let res = match
         .map(v => [...input].filter(c => c === v).length)
         .reduce((a, b) => a + b)

console.log(res);

You can also use for..of loop to iterate each character, String.prototype.contains(), if supported, or .some(), to check if character is within joined string match.

 let n = 0;
 let m = match.join("");
 for (let str of input) m.contains(str) && (n += 1);

for (let str of input) match.some(s => s === str) && (n += 1);
0
1

You can use regexp to find the matching characters:

function escape(chr) { return '\\' + chr; }

input.match(new RegExp(match.map(escape).join('|'), "g")).length

The escaping is necessary to avoid problems with characters such as | which have special meaning in regexps.

2
  • 1
    If the match array consists of values such as ['&', '|', '-', '#'], then it will throw the error /[&|-#]/: Range out of order in character class (example), because |-# is interpreted as a range since it is inside of a character class... You would need to escape the values if you are using this method... ['\\&', '\\|', '\\-', '\\#']. Feb 14, 2017 at 1:52
  • @JoshCrozier Thanks, fixed.
    – user663031
    Feb 14, 2017 at 3:51
1

Since you asked for reduce:

let count = Array.prototype.reduce.call(input, (counter, chr) => (counter + (match.indexOf(chr)!==-1 ? 1: 0)), 0)

UPDATED:

Based on comments on other answers, OP would like the ability to also search substrings like 'abc' in addition to individual characters. Here's a more elegant solution which you can put in a local module.

There are 2 functions that work together: one escapes the special characters in match, the other does the counting. And using reduce again...

    
// This escapes characters
const escapeRegExp = (str) => (
  str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&")
)
    
// This returns the count
const countMatches = (input, match, i = true) => (
  match.reduce((c, pattern) => (
    c + input.match(new RegExp(escapeRegExp(pattern), (i ? 'ig' : 'g'))).length
  ), 0)
)
    
// OP's test case
let match = ['&', '|', '-'],
    input = 'This & That | Or | THIS - Foo',
    count = countMatches(input, match)
console.log('Expects 4: ', count, 4 === count)
    
    
// Case Sensitive Test
match = ['abc']
input = 'This ABC & That abc | Or | THIS - Foo'
    
count = countMatches(input, match, false)
console.log('Expects 1: ', count, 1 === count)
    
// Case Insensitive Test
count = countMatches(input, match)
console.log('Expects 2: ', count, 2 === count)

0

Your Answer

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.