Develop
2017.12.14 16:10
[js] 문자열에서 숫자만 걸러내기 (jQuery 안쓰고 정규표현식)
조회 수 1111 댓글 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
번호 | 분류 | 제목 | 글쓴이 | 날짜 | 조회 수 |
---|---|---|---|---|---|
277 | Develop | 라이브러리에 대한 설명 (static & dynamic library) | hooni | 2013.04.23 | 6946 |
276 | Develop | [js] 네이버, 다음의 플레이어 스킨 | hooni | 2013.04.23 | 6942 |
275 | Develop | [js] php의 number_format() 함수와 같은.. ㅋㅋ | hooni | 2013.04.23 | 6939 |
274 | Develop | [c] 숫자 맞추는 게임.. | hooni | 2013.04.23 | 6935 |
273 | Develop | [c] 도메인(호스트)으로 IP정보 알아오기.. (nslookup과 비슷) | hooni | 2013.04.23 | 6934 |
272 | Develop | [c] 단기과정[01/06] sizeof, 실수표현, 메모리, 연산자 | hooni | 2003.04.23 | 6928 |
271 | Develop | [php] 빔 프로젝터 예약 프로그램.. ㅋㅋ | hooni | 2013.04.23 | 6924 |
270 | Develop | [c] 소켓의 세가지 동작모드 | hooni | 2003.04.23 | 6913 |
269 | Develop | [c] 프로세스 정보 출력하기.. | hooni | 2003.04.23 | 6910 |
268 | Develop | [c] 단기과정[01/10] 과제.. swap(any data, ..) | hooni | 2003.04.23 | 6904 |
267 | Develop | [c] 맵서치인 듯(옛날 컴에서 찾은 자료) | hooni | 2013.04.23 | 6904 |
266 | Develop | [c] pcapdump 파일 분석 하는 프로그램.. ㅋㅋ | hooni | 2013.04.23 | 6903 |