2

I have a regular expression to check for a number of words in a file.

/((word1?)|(word2?)|(word3?)|(word4?)|(word5 ?)|(word6?)|(word7?))/gmi

Is there a way in which I can count the number of matches per word in the the same line f code?

That is , when the code is executed I would want each word tobe counted. (example: word1: 10 matches, word 2, 11 matches...)

javascript novice
  • 777
  • 2
  • 9
  • 28

2 Answers2

2

You can do something like this using replace()

var string = 'word1 word3 word1 word2 word4 word5 word1 word1 word3 word2';
var count = {};

string.replace(/\bword\d+\b/gmi, function($i) {
  count[$i] = count[$i] ? count[$i] + 1 : 1;
});

console.log(count)

Update : If you want all word count then use

var string = 'word1 word3 word1 word2 word4 word5 word1 word1 word3 word2';
var count = {};

string.replace(/\b\w+\b/gmi, function($i) {
  count[$i] = count[$i] ? count[$i] + 1 : 1;
});

console.log(count)

Or if you just need word count of certain word then use

var string = 'word1 word3 word1 word2 word4 word5 word1 word1 word3 word2';
var count = {};

string.replace(/\b(word1|word2|word3|word4|word5|word6|word7)\b/gmi, function($i) {
  count[$i] = count[$i] ? count[$i] + 1 : 1;
});

console.log(count)
Pranav C Balan
  • 113,687
  • 23
  • 165
  • 188
2

You can use the String.prototype.replace() function. It won't be one line of code, but it'll be pretty simple:

var regex = /((word1?)|(word2?)|(word3?)|(word4?)|(word5 ?)|(word6?)|(word7?))/gmi;

var counts = {};
var sourceText = yourSourceTextWithWordsInIt;

sourceText.replace(regex, function(_, matched) {
  matched = matched.toLowerCase();
  counts[matched] = (counts[matched] || 1) + 1;
});

Then the counts object will contain exactly what you described. The .replace() function on the String prototype can take a function as its second parameter. Such a function will be called repeatedly when the pattern has the "g" flag. Each call to the function will include as the first parameter the entire matched substring, and the subsequent parameters will be the parenthesized group matches from the regex.

Pointy
  • 405,095
  • 59
  • 585
  • 614