JavaScript RegEx: Understanding Syllable Counter Code

I have used Dylan's question on here regarding JavaScript syllable counting, and more specifically artfulhacker's answer, in my own code and, regardless of which single or multi word string I feed it, the function is always able to correctly count the number of syllables.

I have a limited experience with RegEx and not enough prior knowledge to decipher what exactly is happening in the following code without some help. I'm not someone who is ever happy with having some code I pulled from somewhere just work without me knowing how it works. Is someone able to please articulate what is happening in the new_count(word) function below and help me decipher the use of RegEx and how it is that the function is able to correctly count syllables? Many

function new_count(word) {
  word = word.toLowerCase();                                     //word.downcase!
  if(word.length <= 3) { return 1; }                             //return 1 if word.length <= 3
  word = word.replace(/(?:[^laeiouy]es|ed|[^laeiouy]e)$/, '');   //word.sub!(/(?:[^laeiouy]es|ed|[^laeiouy]e)$/, '')
  word = word.replace(/^y/, '');                                 //word.sub!(/^y/, '')
  return word.match(/[aeiouy]{1,2}/g).length;                    //word.scan(/[aeiouy]{1,2}/).size
}
Answer:1

As far as I see it, we basically want to count the vowels, or vowel pairs, with some special cases. Let's start by the last line, which does that, i.e. count vowels and pairs:

return word.match(/[aeiouy]{1,2}/g).length;

This will match any vowel, or vowel pair. [...] means a character class, i.e. that if we go through the string character-by-character, we have a match, if the actual character is one of those. {1, 2} is the number of repetitions, i.e. it means that we should match exactly one or two such characters.

The other two lines are for special cases.

word = word.replace(/(?:[^laeiouy]es|ed|[^laeiouy]e)$/, '');

This line will remove 'syllables' from the end of the word, which are either:

  • Xes (where X is anything but any of 'laeiouy', e.g. 'zes')
  • ed
  • Xe (where X is anything but any of 'laeiouy', e.g. 'xe')

(I'm not really sure what the grammatical meaning behind this is, but I guess, that 'syllables' at the end of the word, like '-ed', '-ded', '-xed' etc. don't really count as such.) As for the regexp part: (?:...) is a non-capturing group. I guess it's not really important in this case that this group be non-capturing; this just means that we would like to group the whole expression, but then we do not need to refer back to it. However, we could have used a capturing group too (i.e. (...) )

The [^...] is a negated character class. It means, match any character, which is none of those listed here. (Compare to the (non-negated) character-class mentioned above.) The pipe symbol, i.e. |, is the alternation operator, which means, that any of the expressions can match. Finally, the $ anchor matches the end of the line, or string (depending on the context).

word = word.replace(/^y/, '');

This line removes 'y'-s from the beginning of words (probably 'y' at the beginning does not count as a syllable -- which makes sense in my opinion). ^ is the anchor for matching the beginning of the line, or string (c.f. $ mentioned above).

Note: the algorithm only works if word really contains one single word.

Answer:2

I am doing image editing integration with Aviary. Below is the html code <body> <a href="#" onclick="return launchEditor('editableimage1','http://www.html5canvastutorials.com/demos/assets/...

I am doing image editing integration with Aviary. Below is the html code <body> <a href="#" onclick="return launchEditor('editableimage1','http://www.html5canvastutorials.com/demos/assets/...

Hey I have a login form that when you click on the input the label move up, after focus out it checks if there is text in the input and if have the label stays up if dont the label comes down. My ...

Hey I have a login form that when you click on the input the label move up, after focus out it checks if there is text in the input and if have the label stays up if dont the label comes down. My ...

  1. jquery check input value
  2. jquery check input type
  3. jquery check input checkbox
  4. jquery check input has value
  5. jquery check input text value empty
  6. jquery check input checked
  7. jquery check input length
  8. jquery check input file empty
  9. jquery check input disabled
  10. jquery check input value change
  11. jquery check input pattern
  12. jquery check input number only
  13. jquery check input field not empty
  14. jquery check input value length
  15. jquery check input name
  16. jquery check input valid
  17. jquery check input type checkbox
  18. jquery check input file size
  19. jquery check input type select
  20. jquery check input change

I am creating a custom carousel and its working fine. What I have:- I have a basic code which is working fine and carousel is also working fine in all screen and devices. What I Want:- I have to ...

I am creating a custom carousel and its working fine. What I have:- I have a basic code which is working fine and carousel is also working fine in all screen and devices. What I Want:- I have to ...

  1. bind resize event to div
  2. jquery bind resize event
  3. angular bind resize event
  4. bind window resize event
  5. jquery bind resize event div
  6. angularjs bind resize event

I have a content DIV with many paragraphs. This is how its' markup look, <div class="more"> <p>Based on a study of its skull, scientists say that an extinct one-tonne relative of ...

I have a content DIV with many paragraphs. This is how its' markup look, <div class="more"> <p>Based on a study of its skull, scientists say that an extinct one-tonne relative of ...