728x90
๐ 26 - ์ซ์๋ง ์ถ์ถ
๋ฌธ์ + ์ซ์
๊ฐ ์์ฌ์๋ ๋ฌธ์์ด์์ ์ซ์๋ง ์ถ์ถํ๋ ๋ฌธ์ ๋ค. ์ซ์ํ๋ณ์ isNaN
ํจ์๋ฅผ ์ฌ์ฉํด์ ์ซ์์ธ์ง ํ์ธํ๊ฑฐ๋, ์ ๊ทํํ์(Reg)
์ผ๋ก ์ซ์์ธ์ง ํ์ธํ๋ ๋ฐฉ๋ฒ์ ์ฌ์ฉํ๋ค.
let s = "g0en2T0s8eSoft";
// let s = "tge0a1h205er"
// let s = "12a3E4hg5n6vc7x"
console.log(solution(s));
// isNaN
function solution(s) {
let ans = 0;
for (let i of s) {
if (!isNaN(i)) ans = ans * 10 + (+i);
}
return +ans;
}
//isNaN
function solution(s) {
let ans = "";
for (let i of s) {
if (!isNaN(i)) ans += i;
}
return +ans;
}
//replace + Reg
function solution(s){
let result = s.replace(/[^0-9]/g, '');
return +result;
}
//match + Reg
function solution(s){
let result = s.match(/[0-9]/g).join('');
return +result
}
๋ฐ์ํ
'Algorithm > ์ธํ๋ฐ(inflearn)' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ ์๋ฐ์คํฌ๋ฆฝํธ(JavaScript) ] 28 - ๋ฌธ์์ด ์์ถ (0) | 2021.08.17 |
---|---|
[ ์๋ฐ์คํฌ๋ฆฝํธ(JavaScript) ] 27 - ๊ฐ์ฅ ์งง์ ๋ฌธ์๊ฑฐ๋ฆฌ (0) | 2021.08.17 |
[ ์๋ฐ์คํฌ๋ฆฝํธ(JavaScript) ] 25 - ์ ํจํ ํฐ๋ฆฐ๋๋กฌ (0) | 2021.08.17 |
[ ์๋ฐ์คํฌ๋ฆฝํธ(JavaScript) ] 24 - ํ๋ฌธ ๋ฌธ์์ด (0) | 2021.08.17 |
[ ์๋ฐ์คํฌ๋ฆฝํธ(JavaScript) ] 23 - ๋ด์ฐ๋ฆฌ (0) | 2021.08.17 |
๋๊ธ