在JavaScript中将-后面的所有字符替换为大写:

huangapple go评论72阅读模式
英文:

Replace all chars after - in string to uppercase in JS

问题

我有一个函数

firstLetterUppercase(str: string) {
    let splitStr = str.toLowerCase().split(' ');
    for (let i = 0; i < splitStr.length; i++) {
        splitStr[i] = splitStr[i].charAt(0).toUpperCase() + splitStr[i].substring(1);
    }
    return splitStr.join(' ');
}

它对字符串“Warszawa,Krakow,Bielsko - Biala等”有效。

我需要将它更改为字符串“Bielsko-biala”(我也需要连字符后的第一个字符大写)。

我该如何做?

请帮帮我。

英文:

I have function

firstLetterUppercase(str: string) {
    let splitStr = str.toLowerCase().split(&#39; &#39;);
    for (let i = 0; i &lt; splitStr.length; i++) {
      splitStr[i] = splitStr[i].charAt(0).toUpperCase() + splitStr[i].substring(1);
    }
    return splitStr.join(&#39; &#39;);
  }

It's work fine for string: Warszawa, Krakow, Bielsko - Biala etc.

I need change it to string 'Bielsko-biala'.
(i need first char after '-' uppercase too).

How can i make it?

Please help me

答案1

得分: 1

你可以使用正则表达式和replace()方法来查找并替换连字符后面的小写字母为它们的大写形式。

function firstLetterUppercase(str: string) {
  let splitStr = str.toLowerCase().split(" ");
  for (let i = 0; i < splitStr.length; i++) {
    splitStr[i] =
      splitStr[i].charAt(0).toUpperCase() + splitStr[i].substring(1);
    splitStr[i] = splitStr[i].replace(/-([a-z])/g, (match) =>
      match.toUpperCase()
    );
  }
  return splitStr.join(" ");
}
英文:

You can use a regular expression and the replace() method to find and replace the lowercase letters after a dash with their uppercase counterparts.

function firstLetterUppercase(str: string) {
  let splitStr = str.toLowerCase().split(&quot; &quot;);
  for (let i = 0; i &lt; splitStr.length; i++) {
    splitStr[i] =
      splitStr[i].charAt(0).toUpperCase() + splitStr[i].substring(1);
    splitStr[i] = splitStr[i].replace(/-([a-z])/g, (match) =&gt;
      match.toUpperCase()
    );
  }
  return splitStr.join(&quot; &quot;);
}

答案2

得分: 0

以下是已更新的代码,可用于 'Bielsko-biala'。

在修改后的函数中,我添加了一个条件语句,用于检查当前单词是否包含连字符('-')。如果包含,我使用 indexOf('-') 找到连字符的索引。然后,通过连接字符串的相关部分和它们的大写版本来将连字符后面的字母大写。

function firstLetterUppercase(str) {
  let splitStr = str.toLowerCase().split(' ');

  for (let i = 0; i < splitStr.length; i++) {
    if (splitStr[i].includes('-')) {
      let hyphenIndex = splitStr[i].indexOf('-');
      splitStr[i] =
        splitStr[i].charAt(0).toUpperCase() +
        splitStr[i].substring(1, hyphenIndex + 1) +
        splitStr[i].charAt(hyphenIndex + 1).toUpperCase() +
        splitStr[i].substring(hyphenIndex + 2);
    } else {
      splitStr[i] = splitStr[i].charAt(0).toUpperCase() + splitStr[i].substring(1);
    }
  }

  return splitStr.join(' ');
}

console.log(firstLetterUppercase('Bielsko-biala'))

希望这能满足您的需求。

英文:

Here is the updated code that will work for 'Bielsko-biala'.
In the modified function, I have added a conditional statement to check if the current word contains a hyphen ('-'). If it does, I locate the index of the hyphen using indexOf('-'). Then, it capitalizes the letter immediately after the hyphen by concatenating the relevant portions of the string with their capitalized counterparts.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

function firstLetterUppercase(str) {
  let splitStr = str.toLowerCase().split(&#39; &#39;);

  for (let i = 0; i &lt; splitStr.length; i++) {
    if (splitStr[i].includes(&#39;-&#39;)) {
      let hyphenIndex = splitStr[i].indexOf(&#39;-&#39;);
      splitStr[i] =
        splitStr[i].charAt(0).toUpperCase() +
        splitStr[i].substring(1, hyphenIndex + 1) +
        splitStr[i].charAt(hyphenIndex + 1).toUpperCase() +
        splitStr[i].substring(hyphenIndex + 2);
    } else {
      splitStr[i] = splitStr[i].charAt(0).toUpperCase() + splitStr[i].substring(1);
    }
  }

  return splitStr.join(&#39; &#39;);
}

console.log(firstLetterUppercase(&#39;Bielsko-biala&#39;))

<!-- end snippet -->

答案3

得分: 0

以下是翻译好的代码部分:

function firstLetterUppercase(str) {
  let splitStr = str.toLowerCase().split(' ');
  for (let i = 0; i < splitStr.length; i++) {
    splitStr[i] = splitStr[i].charAt(0).toUpperCase() + splitStr[i].substring(1);
  }

  //Added code
  let result = splitStr.join(' ');
  result = result.replace(/-([a-z])/g, function(match, group1) {
    return '-' + group1.toUpperCase();
  });

  return result;
}

注意:代码部分已经翻译成了中文,如有需要,请使用这个翻译好的代码。

英文:

One way of doing it can be:

function firstLetterUppercase(str) {
  let splitStr = str.toLowerCase().split(&#39; &#39;);
  for (let i = 0; i &lt; splitStr.length; i++) {
    splitStr[i] = splitStr[i].charAt(0).toUpperCase() + splitStr[i].substring(1);
  }

  //Added code
  let result = splitStr.join(&#39; &#39;);
  result = result.replace(/-([a-z])/g, function(match, group1) {
    return &#39;-&#39; + group1.toUpperCase();
  });

  return result;
}

答案4

得分: 0

如果您想要在“单词边界”之后“替换”所有“字符”为它们的“大写形式”,最简单的方法是使用正则表达式:

/**
 * 使用正则表达式(`g` regex flag)替换任何类型的字符(`.`)
 * 在单词边界(`\b`)之后为它们设置大写形式
 */
function firstLetterUppercase(str) {
  return str.replaceAll(/\b./g, c => c.toUpperCase())
}
英文:

If you want to "replace" "all" the "characters" starting after "word border", with their "Upper case"
the easiest way would be using regular expressions:

/**
 * `replaceAll`(`g` regex flag) characters of any type (`.`)
 * after word border (`\b`) with their `toUpperCase`
 */
function firstLetterUppercase(str) {
  return str.replaceAll(/\b./g, c =&gt; c.toUpperCase())
}

huangapple
  • 本文由 发表于 2023年7月3日 19:21:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76604253.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定