[RegExp][chapGPT] 대소문자 변환하기

김정현·2023년 1월 3일
0

기타

목록 보기
17/25

소문자를 대문자로 변환하기

const string = "hello";
const uppercase = string.replace(/[a-z]/g, (char) => char.toUpperCase());
console.log(uppercase);  // "HELLO"

소문자, 숫자, 공백문자를 제외한 모든 문자를 대문자로 변환하기

const uppercase = string.replace(/[^a-z0-9\s]/gi, (char) => char.toUpperCase());

소문자를 제외한 모든 문자 제거하기

const onlyLowercase = string.replace(/[^a-z]/g, "");

대문자와 소문자 뒤바꾸기

const string = "HeLLo WorLD";
const swapped = string.replace(/[a-z]/gi, (char) => (char === char.toUpperCase()) ? char.toLowerCase() : char.toUpperCase());
console.log(swapped);  // "hEllO wOrld"
profile
개발 공부 블로그

0개의 댓글