Skip to content
Advertisement

i want to count each word in a string but skip words that have special chars or numbers in them. ex (“j9[”, “h5”) will be skipped

Each word is a sequence of letters, (‘a- A-Z), that may contain one or more hyphens and may end in a punctuation mark period ), comma ().question mark (?), or exclamation point (1). Words will be separated by one or more white space characters. Hyphens join two words into one and should be retained while the other punctuation marks should be stripped.

here is my code:

function howMany(sentence) {
   
    sentence = sentence.replace(/(^s*)|(s*$)/gi,"");
  
    sentence = sentence.replace(/[0-9 ]{2,} /gi," ");
 
    sentence = sentence.replace(/n /, "/n");

    return sentence.split(' ').length;
}
console.log(howMany("b? Dl )B 4(V! A. MK, YtG ](f 1m )CNxuNUR {PG? "))

the expected output is 5 and mine output give me 11

Advertisement

Answer

You can use

function howMany(sentence) {
  return sentence.split(/s+/).filter(function(x) {
    return /^[A-Za-z]+(?:-[A-Za-z]+)*[.,;:?!]?$/.test(x); 
  }).length;
}
console.log(howMany("b? Dl )B 4(V! A. MK, YtG ](f 1m )CNxuNUR {PG? "))

Details

  • sentence.split(/s+/) splits all text into non-whitespace chunks
  • .filter(function(x) { return /^[A-Za-z]+(?:-[A-Za-z]+)*[.,;:?!]?$/.test(x);}) only keeps the items that match the following pattern:
    • ^ – start of string
    • [A-Za-z]+ – one or more letters
    • (?:-[A-Za-z]+)* – zero or more repetitions of - and 1+ letters
    • [.,;:?!]? – an optional ., ,, ;, :, ? or !
    • $ – end of string
  • (...).length finall fetches the count of these items.

NOTE: You might want to further precise the [.,;:?!] part, add more allowed punctuation here if necessary.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement