Pig Latin help me with regex

Pig Latin help me with regex
0

#1

Tell us what’s happening:
I’m working on the pig latin problem and need help with Regex. If this is the wrong way to approach this problem, please redirect me.
when testing for Regex it returns true or false, right? so if I’m looking for the first instance of a vowel, is there a way I could test where it would give me the index on split where the Regex was met. also I forgot how to test split for the regex1. If I could find the index of where the first vowel occurs, I can use 2 slices of str and concat them with the ‘ay’ to get the correct answer.

Your code so far


function translatePigLatin(str) {
  let split = [...str];
  let regex1 = /a|i|e|o|u|y/;
  let firstVowel = [firstA, firstE, firstI, firstO, firstU];
  console.log(firstVowel);
  if (str.startsWith('a') || str.startsWith('e') || str.startsWith('i') || str.startsWith('o') || str.startsWith('u') || str.startsWith('y')) {
    return str + "way";
  } else{
    return str;
  }
  
}

translatePigLatin("consonant");

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/pig-latin


#2

https://regexr.com/ that is reg clickable constructor you see immediately the reactions on the highlighted text
Yeah, that depends of test function. test is boolean match return
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp

var s = 'Please yes\nmake my day!';
s.match(/yes.*day/);
// Returns null
s.match(/yes[^]*day/);
// Returns ["yes\nmake my day"]
var str = '#foo#';
var regex = /foo/y;

regex.lastIndex = 1;
regex.test(str); // true
regex.lastIndex = 5;
regex.test(str); // false (lastIndex is taken into account with sticky flag)
regex.lastIndex; // 0 (reset after match failure)
var text = 'Some text\nAnd some more\r\nAnd yet\rThis is the end';
var lines = text.split(/\r\n|\r|\n/);
console.log(lines); // logs [ 'Some text', 'And some more', 'And yet', 'This is the end' ]