๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm/์ธํ”„๋Ÿฐ(inflearn)

[ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ(JavaScript) ] 26 - ์ˆซ์ž๋งŒ ์ถ”์ถœ

by YWTechIT 2021. 8. 17.
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
}
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€