Develop
2017.12.14 16:10
[js] 문자열에서 숫자만 걸러내기 (jQuery 안쓰고 정규표현식)
Views 1345 Comment 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
No. | Category | Subject | Author | Date | Views |
---|---|---|---|---|---|
465 | Develop | [js] 모바일 스크롤 방지(해제) | hooni | 2015.04.14 | 1686 |
464 | Develop | [js] 모바일웹에서 이미지 저장하는거 (context menu) 막기 | hooni | 2013.04.23 | 26416 |
» | Develop | [js] 문자열에서 숫자만 걸러내기 (jQuery 안쓰고 정규표현식) | hooni | 2017.12.14 | 1345 |
462 | Develop |
[js] 밀리터리 프로그램(전역일 계산)
7 ![]() |
hooni | 2013.04.23 | 8923 |
461 | Develop |
[js] 부모창에서 자식창으로 문자열 전달..
![]() |
hooni | 2013.04.23 | 7471 |
460 | Develop |
[js] 비만 지수 측정(BMI) ㅎㅎ
![]() |
hooni | 2013.04.23 | 7628 |
459 | Develop |
[js] 빈도우즈(bindows96)
![]() |
hooni | 2013.04.23 | 7390 |
458 | Develop | [js] 사라지는 브라우저 | hooni | 2003.04.23 | 6877 |
457 | Develop | [js] 사진첩에 쓸 내용 - 마우스 오버로 바꾸기 | hooni | 2013.04.23 | 6356 |
456 | Develop | [js] 새로고침(refresh)방법과 다른 페이지 바꾸기.. | hooni | 2003.04.23 | 6562 |
455 | Develop | [js] 서서히 나타나는 화면.. ㅋㅋ | hooni | 2013.04.23 | 6218 |
454 | Develop |
[js] 셀렉트박스(select)의 옵션(option) 동적으로 추가/제거
![]() |
hooni | 2013.04.23 | 8074 |