Saturday, 15 August 2015

javascript - change double letters in string -


I am creating a function that can return an array with the characters of a word. It needs to save a few double letters as one of the arrays. I have an array that contains double letters. I have a list of words (some time ago) The problem is now that it saves the letter 2 times for the first time and it saves double characters from the word in the alphabet from the letter. The function itself is running in a loop, which receives the word from the JSN object. The ceremony is made in a way that I could think, but if there is a better way (clearly) please tell me how how.

Function:

  var word = 'voorheen'; Var doubles = ['A', 'O', 'E', 'I', 'O', 'EU', 'U', 'Au', 'OU', 'UI', 'NG', 'C ',' IJ ']; Var characters = getLetters (word, couple); Console.log (letter); Function getLetters (words, doubles) {var characters = []; Var special = []; Var Indexes = []; (Var s = 0; s & lt; doubles.length; s ++) {if (word.indexOf (couple [s])! = -1) {special. Peash (doubles [S]); Indexes.push (word.indexOf (doubles [s])); Console.log ('special:' + special); Console.log ('index:' + indexed); }} For (var i = 0; i & lt; word.length; i ++) {if (specials.length & gt; 0) {for (var j = 0; j & lt; special; length; j ++) {if (i & lt; index [j]) {letters.push (word [i]); Console.log ('i:' + i); Console.log ('j:' + j); Console.log ('Letter:' + [i]); } If (I == index [ja]) {letters.push (special [j]); Console.log ('i:' + i); Console.log ('j:' + j); Console.log ('Letter:' + [i]); } If (i> indexed [j] + special [j] length) {letters.push (word [i]); Console.log ('i:' + i); Console.log ('j:' + j); Console.log ('Letter:' + [i]); }}} And {letters.push (term [i]); }} Return letter; }   

Chrome log output:

  ["v", "v", "oo", "o", "o", " Code> ["V", "O", "R", "H", "EE", and / or P.> 

, "N"]

  var word = 'voorheen'; Var doubles = ['A', 'O', 'E', 'I', 'O', 'EU', 'U', 'Au', 'OU', 'UI', 'NG', 'C' 'Ij']; Var characters = word match (new regxp (doubles.joined ("|") + ".", "G")) [];   

Regexes Capture as much as possible (unless it has been specifically announced), then the two-letter combinations are . , which matches any single character.

Bonus points because it can be expanded to include a combination of any length: P

No comments:

Post a Comment