Sunny-117 / js-challenges

✨✨✨ Challenge your JavaScript programming limits step by step

Home Page:https://juejin.cn/column/7244788137410560055

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

字符串的大小写取反

Sunny-117 opened this issue · comments

let str = "QIANDUANGOngchengshi猕猴桃Jquery很帅!哈哈 haha";
str = str.replace(/[a-zA-Z]/g, (content) => {
  return content.toUpperCase() === content
    ? content.toLowerCase()
    : content.toUpperCase();
});
console.log(str);
let str = "QIANDUANGOngchengshi猕猴桃Jquery很帅!哈哈 haha";

let result = "";

for (let i = 0; i < str.length; i++) {
  let char = str.charAt(i);
  if (char === char.toUpperCase()) {
    result += char.toLowerCase();
  } else {
    result += char.toUpperCase();
  }
}

console.log(result); // "qianduangONGCHENGSHI猕猴桃jQUERY很帅!哈哈 HAHA"
let str = "React components wrap existing native code and interact with native APIs via React’s declarative UI paradigm and JavaScript. This enables native app development for whole new teams of developers, and can let existing native teams work much faster."
str = String(str).replace(/([a-zA-z])/g, function(text){
    return text.toLowerCase() === text ? text.toUpperCase() : text.toLowerCase()
})
console.log(str)