You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JavaScript字符串大小写转换及元音保留规则实现问询

Solution for JavaScript String Transformation

Got it, let's tackle this string transformation problem head-on. Below is a complete implementation that follows all four rules you outlined, plus explanations of how each part works.

The Code

function transformString(str) {
    const vowels = new Set(['a', 'e', 'i', 'o', 'u']);
    
    // Helper to check if a character is a vowel (case-insensitive)
    function isVowel(char) {
        return vowels.has(char.toLowerCase());
    }
    
    // Keep only the first two vowels in a word, retain all consonants
    function limitVowels(word) {
        let vowelCount = 0;
        let result = '';
        for (const char of word) {
            if (isVowel(char)) {
                if (vowelCount < 2) {
                    result += char;
                    vowelCount++;
                }
                // Skip any additional vowels
            } else {
                result += char;
            }
        }
        return result;
    }
    
    // Split the string into words, process each, then rejoin
    return str.split(' ').map(word => {
        if (!word) return word; // Handle empty strings if present
        
        // Check original word's start/end for case rules
        const startsWithVowel = isVowel(word[0]);
        const endsWithVowel = isVowel(word[word.length - 1]);
        
        // First apply vowel limiting to the word
        const modifiedWord = limitVowels(word);
        
        // Apply case transformation based on original word's properties
        if (startsWithVowel && endsWithVowel) {
            return modifiedWord.toLowerCase();
        } else if (startsWithVowel && !endsWithVowel) {
            return modifiedWord.toUpperCase();
        } else if (!startsWithVowel && endsWithVowel) {
            // Title case: first letter uppercase, rest lowercase
            return modifiedWord[0].toUpperCase() + modifiedWord.slice(1).toLowerCase();
        } else {
            // For words that don't fit the first three rules, keep modified word as-is
            return modifiedWord;
        }
    }).join(' ');
}

// Example usage with your input string
const str = "i dont know dude i am not typing ALL OF this Nonsens text";
console.log(transformString(str));
// Output: "i dont know Dude i AM not typing ALL OF this Nonsens text"

How It Works

Let's break down each component to make sure you understand:

  • Vowel Check: The isVowel function uses a Set for fast lookups to determine if a character is a vowel (case-insensitive).
  • Vowel Limiting: The limitVowels function iterates through each character in the word. It keeps consonants as-is, and only retains the first two vowels it encounters—any extra vowels are skipped.
  • Case Transformation: For each word, we first check the original word's starting and ending characters to decide which case rule to apply. We then apply the vowel limit before converting to the required case (this ensures we don't accidentally change the start/end character that determined our case rule).
  • Edge Cases: We handle empty strings (in case of multiple spaces) and words that don't fit the first three rules (neither start nor end with a vowel) by returning the modified word as-is.

Testing the Example

When you run the example code, it produces exactly the output you provided:

"i dont know Dude i AM not typing ALL OF this Nonsens text"

This matches all your rules perfectly:

  • "i" stays lowercase (starts/ends with vowel)
  • "am" becomes uppercase (starts with vowel, ends with consonant)
  • "dude" becomes title case (starts with consonant, ends with vowel)
  • All words only keep their first two vowels (none of the example words have more than two, so no visible changes here, but it works for longer words too!)

内容的提问来源于stack exchange,提问作者user6311492

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 06:46:52