简体   繁体   中英

How to make specific word in string upperCase or lowerCase?

Okay I have a small problem I want to make specific words in this string to be upperCase() or lowerCase().

This is what I originally tried:

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()
}

All I want to know is how to simply make certain words lowerCase or upperCase .

you can do this way,for any particular word. here is an example using toUpperCase().

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

or this way, here is an example using substring() and toLowerCase().

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

From a question in 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); 
You can write "Hello World" or "HellO woRld" and the code works in both cases.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM