2016-01-19 37 views
-1

好吧我有一個小問題,我想在這個字符串中的特定單詞是upperCase()或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() 
} 

所有我想知道的是如何只是讓某些詞lowerCaseupperCase

+0

您可以使用正則表達式來查找和修改這些單詞,這些單詞是什麼? – Filipe

+0

某些單詞?究竟哪些? – nicael

+0

如果你計劃離開一個例子,請選擇任何兩個單詞..沒關係哪兩個。 –

回答

0

你可以這樣做,對於任何特定的單詞。 這裏是一個使用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()); 
+1

謝謝!謝謝偉大的答案!簡單...正是我想要的! –

0

在StackOverflow的一個question

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」 的和代碼工作 在這兩種情況下。

相關問題