Results 1 to 3 of 3

Thread: Regular Expression: "ng" followed by [aeiou] should be a match by itself

  1. #1

    Thread Starter
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Question 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.
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  2. #2
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,958

    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.

  3. #3

    Thread Starter
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    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
    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.
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width