get the neighboring characters that are not vowels

2

I want to obtain the neighboring characters that are not vowels, but it does not show me all, the code below is what I try but it does not fulfill my objective.

function solve(s) {
    s = s.split('');
    let res = '';
    let array = [];
    array = s.map((value, idx, arr) =>
        (!vocal(value) && !vocal(arr[idx - 1]) && typeof arr[idx - 1] !== 'undefined') ?
            res + value : res = ''
    )

    console.log(array);
    return 0;
};

function vocal(a) {
    let res = false;
    switch (a) {
        case 'a':
            res = true;
            break;
        case  'e':
            res = true;
            break;
        case  'i':
            res = true;
            break;
        case 'o':
            res = true;
            break;
        case 'u':
            res = true;
            break;
    }
    return res;
}

solve('hhollliillapiiii');

Expected result: [hh, lll, ll], the p character has no neighbor that is not a vowel so it should not enter.

    
asked by hubman 13.03.2018 в 21:09
source

3 answers

4

You can use str.match (regexp) with a regular expression to find all substrings of at least 2 non-vowel characters:

/([^aeiou]{2,})/gi

Although if you only want letters you would have to change the expression

function solve(s) {
    array = s.match(/([^aeiou]{2,})/gi);
    console.log(array);
    return 0;
};

solve('hhollliillapiiii');
    
answered by 13.03.2018 / 21:41
source
2

The easiest way is to make a replace of the string:

var s = 'hhollliillpiiii';
var sinvocales = s.replace(/[aeiouAEIOU]/g, '');
console.log(sinvocales); // imprime "hhlllllp"

If what you need is an array of each character, then:

var s = 'hhollliillpiiii';
var sinvocales = s.match(/[^aeiouAEIOU]/g);
console.log(sinvocales); // imprime ["h", "h", "l", "l", "l", "l", "l", "p"]
    
answered by 13.03.2018 в 21:22
2

A solution using regular expressions

var cadena = 'hhollliillapiiii';
var salida = cadena.match(/(?![aeiou])([^aeiou])+([^aeiou])/g)
console.info(salida)
    
answered by 13.03.2018 в 22:08