Develop
2017.12.14 16:10
[js] 문자열에서 숫자만 걸러내기 (jQuery 안쓰고 정규표현식)
조회 수 1108 댓글 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
번호 | 분류 | 제목 | 글쓴이 | 날짜 | 조회 수 |
---|---|---|---|---|---|
717 | Develop | [c] 신기한 atoi함수(www.game79.net) | hooni | 2003.04.23 | 7274 |
716 | Develop | [js] 초간단 현재 사이트 쿠키 확인하는 명령~ | hooni | 2003.04.23 | 7275 |
715 | Develop | [pdf] C++ 국제 표준 문서 | hooni | 2013.04.23 | 7281 |
714 | Develop | [c++] mfc로 만든 사용자 계정 리스트 출력(EnumUser) 프로그램 예제 | hooni | 2013.04.23 | 7290 |
713 | Develop | [c]디렉토리 탐색 | hooni | 2003.04.23 | 7303 |
712 | Develop | [jsp] 정적/동적(차트생성) 이미지 전달 | hooni | 2003.04.23 | 7307 |
711 | Develop | 객체지향 프로그래밍에 대한 개념.. (객체) | hooni | 2013.04.23 | 7310 |
710 | Develop | [php] 이미지 회전시키기(gd) | hooni | 2013.04.23 | 7318 |
709 | Develop | [c] 플러드 필링 (flood filling) 알고리즘.. | hooni | 2013.04.23 | 7319 |
708 | Develop | [c/c++] Makefile 사용하기.. ㅋㅋ | hooni | 2003.04.23 | 7321 |
707 | Develop | [c] 스택/힙 오버플로우 테스트(overflow) | hooni | 2003.04.23 | 7324 |
706 | Develop | [ios] How To Use UIScrollView to Scroll and Zoom Content (Using Swift) | hooni | 2016.03.23 | 7348 |