Develop
2017.12.14 16:10
[js] 문자열에서 숫자만 걸러내기 (jQuery 안쓰고 정규표현식)
조회 수 1345 댓글 0
How to Filter Numbers Of A String
Not really jQuery at all:
number = number.replace(/\D/g, '');
That regular expression, /\D/g
, matches any non-digit. Thus the call to .replace()
replaces all non-digits (all of them, thanks to "g") with the empty string.
edit — if you want an actual *number value, you can use parseInt()
after removing the non-digits from the string:
var number = "number32"; // a string
number = number.replace(/\D/g, ''); // a string of only digits, or the empty string
number = parseInt(number, 10); // now it's a numeric value
If the original string may have no digits at all, you'll get the numeric non-value NaN
from parseInt
in that case, which may be as good as anything.
[출처] https://stackoverflow.com/questions/4460595/jquery-filter-numbers-of-a-string
번호 | 분류 | 제목 | 글쓴이 | 날짜 | 조회 수 |
---|---|---|---|---|---|
1029 | Develop | [jsp] Get방식, Post방식 전송 예제.. | hooni | 2003.04.23 | 13791 |
1028 | Develop | [c++] 기초강좌 #01(입출력,영역지정) | hooni | 2003.04.23 | 11458 |
1027 | Develop | [c++] 기초강좌 #02(레퍼런스,메모리할당) | hooni | 2003.04.23 | 11473 |
1026 | Develop | [c++] 기초강좌 #03(함수의 매개변수 전달) | hooni | 2003.04.23 | 12128 |
1025 | Develop | [c++] 기초강좌 #04(클래스) | hooni | 2003.04.23 | 11860 |
1024 | Develop | [c] C로 구현한 CGI - 방명록 | hooni | 2003.04.23 | 33500 |
1023 | Develop | [c] C로 구현한 CGI - 계산기 (링크리스트, 스택, 이진트리) | hooni | 2003.04.23 | 28142 |
1022 | Develop | [c] C로 구현한 CGI - 달력과 날짜계산기 | hooni | 2003.04.23 | 32434 |
1021 | Develop | [jsp] 유효성체크(Client, Server 에서) | hooni | 2003.04.23 | 7456 |
1020 | Develop | [js] 키보드 아스키 코드 확인하는 간단한 소스 | hooni | 2003.04.23 | 7787 |
1019 | Develop | [js] 핫키(단축키) 구현방법 | hooni | 2003.04.23 | 7649 |
1018 | Develop | [js] 이미지 미리 로딩하기 | hooni | 2003.04.23 | 7033 |