Notice
Recent Posts
Recent Comments
«   2024/10   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
Archives
관리 메뉴

함번보고 두번보고

[Javascript] /\d/.test(c) 숫자판별하기! 본문

Front-End/Javascript

[Javascript] /\d/.test(c) 숫자판별하기!

Hamstar_ 2021. 2. 28. 00:51

매번 알고리즘 문제를 풀때마다 느끼지만.. 정규식을 잘쓰자..!

 


* const isNum = c => /\d/.test(c);

let name = "hello, This is hamstar";
let old = "I'm 30 years old...";

const isNum = c => /\d/.test(c);

console.log(isNum(name));
console.log(isNum(old));

// false
// true

 

+ String이 숫자일 때, number로 변환을 하고 싶을 경우 ( parseInt (str) )

let str = "30";

const isNum = c => /\d/.test(c);

if(isNum(str)) {
    console.log(parseInt(str));
    console.log(typeof parseInt(str));
}

// 30
// number

 

+ RegExp Character classes

\d Matches any digit (Arabic numeral). Equivalent to [0-9]. For example, /\d/ or /[0-9]/ matches "2" in "B2 is the suite number".

from MDN

Comments