Regular Expression: "ng" followed by [aeiou] should be a match by itself
The first commented regular expression almost fits my need, except I need it to find "ng" followed by either a,e,i,o,u as a match itself. Been trying for hours to no avail.
Example:
tanga should return "ta" and "nga"
nganga = nga nga
amboy = am boy
tomboy = tom boy
Code:
var bla;
bla = "nganga";
var result;
//var syllableRegex = /[^aeiou]*[aeiou](?:[^aeiou]*$|[^aeiou](?=[^aeiou]))?/gi;
var syllableRegex = /[^aeiou]*[aeiou](?:[ng](?=aeiou)*$|[^aeiou]*$|[^aeiou](?=[^aeiou]))?/gi
while (result = syllableRegex.exec(bla))
{
var Match = result[0];
alert(Match);
alert(syllableRegex.lastIndex);
}
Any help will be greatly appreciated.
Re: Regular Expression: "ng" followed by [aeiou] should be a match by itself
Not quite sure I follow what the regex is trying to do, I can see how the "nganga" examples matches "ng" followed by a vowel but I am not sure how the others fit into the pattern.
Re: Regular Expression: "ng" followed by [aeiou] should be a match by itself
This is the original regex I am using:
Code:
/[^aeiou]*[aeiou](?:[^aeiou]*$|[^aeiou](?=[^aeiou]))?/gi
Quote:
First, zero or more consonants: [^aeiou]*
Then, one or more vowels: [aeiou]+
After that, zero or one of the following:
Consonants, followed by the end of the word: [^aeiouy]*$
A consonant (if it is followed by another consonant): [^aeiouy](?=[^aeiouy])
As I've said, it almost fit my need but I need to modify it so that "nga, nge, ngi, ngo, ngu" should be their own match, disregarding the expression above. Here is what I came up but it returns the match of "^aeiou]*[aeiou]" followed by "nga, nge, ngi, ngo, ngu":
Code:
/[^aeiou]*[aeiou](?:(nga|nge|ngi|ngo|ngu)|[^aeiou]*$|[^aeiou](?=[^aeiou]))?/gi;
My workaround is to divide the returned word:
Code:
var NGPosition = Match.indexOf("ng");
if (NGPosition > 0)
{
var str1 = Match.substr(0,NGPosition);
var str2 = Match.substr(NGPosition, Match.length - NGPosition);
It appears to work in my initial tests but I would really like to make it a regex if possible.