Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/470.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在最后一个元音字符之后,要附加;xy“;_Javascript_Jquery_String - Fatal编程技术网

Javascript 在最后一个元音字符之后,要附加;xy“;

Javascript 在最后一个元音字符之后,要附加;xy“;,javascript,jquery,string,Javascript,Jquery,String,我的应用要求是,如果一个单词有一个像“anupam”这样的元音,那么在最后一个元音后加上“xy”。所以它的结果应该是“anupxy”。但如果一个单词中有两个元音继续,那么应该是这样的: Oa = Oaxy and au = auxy Gardenhire = Gardxy Parker = Parkxy Arney = Arnxy There are 4 rules. 1. The name cuts off at the second vowel and is rep

我的应用要求是,如果一个单词有一个像“anupam”这样的元音,那么在最后一个元音后加上“xy”。所以它的结果应该是“anupxy”。但如果一个单词中有两个元音继续,那么应该是这样的:

      Oa = Oaxy
    and au = auxy
Gardenhire = Gardxy

Parker = Parkxy

Arney = Arnxy

There are 4 rules.
1. The name cuts off at the second vowel and is replaced by 'xy.'
2. connected vowels count as a single and should stay together.
3. EXCEPTION: when the last letter is 'x' another 'x' is not added.
4. Names with only two connected vowels should have "xy" added to the end
我不知道我的错误在哪里,我的代码是:

function doit(userName) {
        var temp = userName.toLowerCase();
        var vowels = "aeiouy"
        var count = 0;

        if(userName) {
            for(var i=0; i<temp.length; i++) {

                if( vowels.indexOf(temp.charAt(i)) > -1 ) {
                    count++;
                    if(count==1) {
                        while( vowels.indexOf(temp.charAt(++i)) != -1 );
                        i--;
                    } else
                        break;
                }
            }

            userName = userName.substr(0, i);
            if( userName.charAt(userName.length-1) == 's' )
                userName += "y";
            else
                userName += "sy";
        } else
            userName = 'Take a lap, Dummy';
        return userName.toUpperCase();

    }
函数doit(用户名){
var temp=userName.toLowerCase();
var元音=“aeiouy”
var计数=0;
如果(用户名){
对于(变量i=0;i-1){
计数++;
如果(计数=1){
while(元音索引)(临时字符(++i))!=-1;
我--;
}否则
打破
}
}
userName=userName.substr(0,i);
if(userName.charAt(userName.length-1)='s')
用户名+=“y”;
其他的
用户名+=“sy”;
}否则
用户名='绕一圈,笨蛋';
返回userName.toUpperCase();
}

我建议您使用正则表达式,尤其是在使用解释语言时。更简单的代码,可能还有更好的性能。见:

像这样:

function doit(userName) {
    var str = userName || "";
    var vowels = ["a","e","i","o","u"];
    var suffix = "xy";

    var str_arr = str.split("");
    var str_arr_rev = str_arr.reverse();

    $.each(str_arr_rev, function (i, item) {
        if ($.inArray(item.toLowerCase(), vowels) > -1) {
            last_vowel_index = i;
            return false;
        }
    });

    if (last_vowel_index == -1) {
        $.each(str_arr_rev, function (i, item) {
            if (item.toLowerCase() == "y") {
                last_vowel_index = i;
                return false;
            }
        });
    }

    if (last_vowel_index > -1)
        str_arr_rev[last_vowel_index] = str_arr_rev[last_vowel_index] + suffix;

    return str_arr_rev.reverse().join("");
}

正则表达式是一种方法

var word = "anumap";
var transformed = word.replace(/(\w+[aeiou]+).*/i, " $1xy");
我创建了一个交互式小提琴:

“\w+”表示匹配任何单词字符。这将得到最后一个元音之前的所有字母。 其中的[]和元音是我们要查找的,括号外的+表示必须至少有一个元音。 “*”表示匹配下一个元音(最后一个元音之后的任何元音)
括号表示将其捕获到变量($1)中。

不确定需求的第二部分

但如果一个单词中有两个元音继续,那么应该是这样的:

Oa = Oaxy and au = auxy -Anup
试试这个模式

html

JSFIDLE

另见


不要使用break。断路器用于开关statement@C-链接号。。是流量控制语句…@ArunPJohny没有标签和开关语句,break可以吗?@C-link:可以,这与break完全兼容。OP中的
中断
用于过早退出
for
循环(当
计数
!=
1
)。@ArunPJohny同样,OP在if-else条件下使用中间括号,有时不使用,这种方式对吗?我认为在最后一个条件下有一个问题。不错的正则表达式,但实际上正则表达式可能是一个糟糕的选择,因为它很难阅读和维护。这是一个例子,说明正则表达式的目的和逻辑的注释绝对必须伴随着代码。你是对的@ZacharyBurt,我在ipad上写作,这被证明是一个糟糕的想法,所以我变得懒惰了。我补充了一个解释。如果有什么不合理的地方,请询问。然而,我认为这个相对简单的正则表达式比上面的doit()函数更容易阅读和维护;)
   <input type="text" value="" /><br />
   <div id="name"></div>
$(function () {
    $("input").on("change", function(e) {       
    $("#name")
        .html(function (index, o) {
        var v = /[aeiou]+.$/gi;
        var o = $(e.target).val();
        var n = v.test(o);
        return (n ? String(o.replace(/[^aeiou]$/gi, "") + "xy").toUpperCase() : o)
    });
   });
})