<html>
<head>
<meta charset="UTF-8">
<title>출력결과</title>
</head>
<!-- A를 #으로 바꾸는 프로그램을 작성 -->
<body>
<script>
let str = "BANANA";
function solution(str) {
let answer ='';
for (let x of str) {
if (x == 'A') answer += '#';
else answer += x;
}
return answer;
}
function solution2(str) {
let answer = str;
answer = answer.replace(/A/g, "#");
return answer;
}
console.log(solution(str));
</script>
</body>
</html>
solution2 는 정규식입니다.
/A/g : /A/ 문자열을 찾는데 g를 붙여 전체로..
answer.replace(/A/g, "#") : answer의 A 전부를 #으로 바꿉니다.
'개발공부 > 알고리즘' 카테고리의 다른 글
[Algorithm] 대문자 찾기 (정규식 포함) (0) | 2023.04.21 |
---|---|
[Algorithm] 특정 문자 개수 찾기 (0) | 2023.04.21 |
[Algorithm] 일곱난쟁이 (0) | 2023.04.20 |
[Algorithm] 차량 10부제 (0) | 2023.04.19 |
[Algorithm] 홀수의 합과, 최솟값 구하기 (0) | 2023.04.19 |