-2

I am trying to capture the counts associated with the keywords in the string txt. All the keywords are loaded into an array ahead of time.

This code is in jquery/javascript. I cannot hard code string keywords so that is why they are stored in an array. Please assist me in finding what goes in place of "Reg Expression" before and/or after the keyword variable within the loop.

The html br can be used to end that regexmatch in that iteration of the loop.

Trying to end up with keywordCount = "2, 5, 11"

//String I need to search through
var txt = "Edit Req'd2<br>Errors5<br>Pndg App11<br>";

//array of keywords I can use to find associated counts to keywords
var keyword = ["Edit Req'd", "Errors", "Pndg App"];

//empty string declared before loop
var keywordCount = '';

for (i = 0; i < keyword.length; i++) {

    // takes the comma off end of first entry in array
    // might not be needed or another method might be better?
    keyword[i] = $.trim(keyword[i]);

    //regex expression generated using keyword and unknown expression 
    var regexmatch = RegExp("Reg Expression" + keyword + "Reg Expression")

    //use regex expression to generate string containing counts
    keywordCount += (txt.match(regexmatch)) + ",";
}
JMizzle
  • 17
  • 4
  • Possible duplicate of [How to find a number in a string using JavaScript?](https://stackoverflow.com/questions/1623221/how-to-find-a-number-in-a-string-using-javascript) – Robiseb Oct 23 '18 at 12:55
  • Try: var regexmatch = RegExp(keyword[i] + "(\d+)"), then grap Group 1. – Poul Bak Oct 23 '18 at 13:07

2 Answers2

0

I would use a digit/numeric range to match the number ([0-9]; this is pretty basic regex stuff), and use a group to match any of the keywords:

  • Any number of digits: [0-9]+
  • Any of your keywords: (something|somethingelse|other)

You can use capture-groups as well to have match() return them separately:

var keyword = ["Edit Req'd", "Errors", "Pndg App"];
var regexmatch = RegExp('(\b' + keyword.join('|') + ')([0-9]+)', 'g')

(note that we use the word-boundary \b to make sure keyword is not part of a longer word, and the 'g' flag for global to signal we want multiple results)

Now, match() will only match one result, and we want matches for every keyword, so we’ll use exec() instead:

var txt = "Edit Req'd2<br>Errors5<br>Pndg App11<br>";
var keyword = ["Edit Req'd", "Errors", "Pndg App"];
var regex = RegExp('(\b' + keyword.join('|') + ')([0-9]+)', 'g')

var match = regex.exec( txt ); // ...find the first match
var numbers = [];

while ( match !== null ) {
  numbers.push( match[ 2 ]); // #0 of exec() its return-value is the result as a whole: "Edit Req'd2"
                             // #1 is the value of the first capture-group: "Edit Req'd"
                             // #2 is the value of the second capture-group: "2"

  match = regex.exec( txt ); // ...find the next match
}

console.log("the numbers are:", numbers);

Lastly, do note that regular expressions may look cool, they are not always the fastest (performance-wise). If performance matters a lot, you could use (for example) indexOf() instead.


From your question it seems like you could brush up on your knowledge of regular expressions a little bit. There’s a ton of articles around (just search for “regular expressions basics” or “regex 101”) – like this one: https://medium.com/factory-mind/regex-tutorial-a-simple-cheatsheet-by-examples-649dc1c3f285*

0

Here is the example which may helps you in achieving your required output.

//String I need to search through
var txt = "Edit Req'd2<br>Errors5<br>Pndg App11<br>";

//array of keywords I can use to find associated counts to keywords
var keyword = ["Edit Req'd", "Errors", "Pndg App"];

//empty string declared before loop
var keywordCount = '';
var i = 0;
var keywordCount = "";
var splittedValue = "";
while (i < keyword.length) {
    if (txt.indexOf(keyword[i]/i)) {
        splittedValue = txt.split(keyword[i]);
        if (keywordCount === "") {
            keywordCount = splittedValue[1].split("<br>")[0];
        } else {
            keywordCount += ", " + splittedValue[1].split("<br>")[0];
        }
    }
    i += 1;
}
console.log(keywordCount);
Sami Ahmed Siddiqui
  • 2,328
  • 1
  • 16
  • 29