Bilal Hussain Bilal Hussain - 17 days ago 5
Javascript Question

Count the number of words in the line beginning with a particular word

I want to count the number of words in a particular line which contains a specific ID (e.g. *AUY). So far I have tried using the below regex for finding the line but it does not consider the "*" at the start

^ *(.*\b(?:\\*AUY)\b.*) *$


I have below test string

*AUY: today is holiday so Peter and Mary do not need to go to work .
%mor: n|today cop|be&3s n|holiday conj|so n:prop|Peter conj|and n:prop|Mary v|do neg|not v|need inf|to v|go prep|to n|work .
%snd: <00:00:00><00:07:37>
%AUY: ok_pfp (0.40) er today is holiday errfr ::: so er Peter and Mary {is} ~ er do not need errfr ::: to go to work . errfr :;:a |


The result should be only first string but it returns first and last string in result matches.
See this Rubular

Answer

Try that:

/^.*?\*AUY:(.*?)$/gmi

Explanation

  1. ^ asserts position at start of a line
  2. .*? matches any character (except for line terminators)
  3. *? Quantifier — Matches between zero and unlimited times (lazy)
  4. \* matches the character *
  5. AUY: matches the characters AUY
  6. .*? matches any character (except for line terminators)
  7. $ asserts position at the end of a line
  8. g modifier: global. don't return after first match
  9. m modifier: multi line. Causes ^ and $ to match the begin/end of each line (not only begin/end of string)
  10. i modifier: insensitive

Rubular

Code Sample:

function countWord(){

const regex = /^.*?\*AUY:(.*?)$/gmi;
const str = `*AUY:  today is holiday so Peter and Mary do not need to go to work .
%mor:   n|today cop|be&3s n|holiday conj|so n:prop|Peter conj|and n:prop|Mary v|do neg|not v|need inf|to v|go prep|to n|work .
%snd:   <00:00:00><00:07:37>
%AUY:   ok_pfp (0.40) er today is holiday errfr ::: so er Peter and Mary {is} ~ er do not need errfr ::: to go to work . errfr :;:a |`;
let m;

while ((m = regex.exec(str)) !== null) {
    // This is necessary to avoid infinite loops with zero-width matches
    if (m.index === regex.lastIndex) {
        regex.lastIndex++;
    }
    alert(m[1].match(/\b(\w+)\b/g).length);
}

    }
Comments