簡體   English   中英

如何在字符串upperCase或lowerCase中制作特定單詞?

[英]How to make specific word in string upperCase or lowerCase?

好的,我有一個小問題,我想在此字符串中指定特定的單詞為upperCase()或lowerCase()。

這是我最初嘗試的:

function lcFunct() {
        var a = "WelCome TO the Test I WANT all THIS to be LoWeRCAse"
        document.getElementById("tests").innerHTML = a.str.replace(6,12).toUpperCase()
}

我只想知道如何簡單地將某些單詞upperCase lowerCaseupperCase

您可以針對任何特定字詞執行此操作。 這是使用toUpperCase()的示例。

var text = "WelCome TO the Test I WANT all THIS to be LoWeRCAse";
text=text.replace("WelCome","WelCome".toUpperCase());

或以這種方式,這是一個使用substring()和toLowerCase()的示例。

text=text.replace(text.substring(0,7),text.substring(0,7).toLowerCase());

來自StackOverflow中的一個問題

 var searchMask = "HELLO WORLD"; var str="Look this HELLO WORLD and this hello world and say HELLO WORLD"; var replaceMask = searchMask.toLowerCase(); var regEx = new RegExp(searchMask, "ig"); var result = str.replace(regEx, replaceMask); alert(result); 
您可以編寫“ Hello World”或“ HellO woRld”,並且代碼在兩種情況下均有效。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM