함번보고 두번보고
[Javascript] /\d/.test(c) 숫자판별하기! 본문
매번 알고리즘 문제를 풀때마다 느끼지만.. 정규식을 잘쓰자..!
* 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
'Front-End > Javascript' 카테고리의 다른 글
[javascript] Class vs Object.. 무슨 차이인고? (0) | 2021.03.05 |
---|---|
[regex] 철이 없었죠..정규식을 모르고 코딩하고있다는거 자체가.. (0) | 2021.03.03 |
[Javascript] Map객체의 Value 최댓값 구하기.(by reduce()) (0) | 2021.02.28 |
[Javascript] Array.Map((v) => ???) 배열 요소 변환하기! (0) | 2020.09.25 |
Comments