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
번호 | 분류 | 제목 | 글쓴이 | 날짜 | 조회 수 |
---|---|---|---|---|---|
357 | Etc | 선과 악에 대한 영어논술문항(지킬앤하이드 독서 후 이어지는 심화 수행평가) | hooni | 2013.12.04 | 12748 |
356 | Develop | [js] Closure를 이용해 캡슐화.. | hooni | 2013.12.16 | 9596 |
355 | Develop | [js] jQuery 코드 작성시 편리한 HTML 템플릿 | hooni | 2013.12.17 | 33073 |
354 | Develop | [js] jQuery 셀랙터(selector) 요약 | hooni | 2013.12.17 | 9420 |
353 | Develop | [js] jQuery 배열 루프(each) | hooni | 2013.12.17 | 9989 |
352 | Develop | [js] 객체 머지.. | hooni | 2013.12.17 | 8965 |
351 | Develop | [js] jQuery 충돌 회피 | hooni | 2013.12.17 | 38315 |
350 | Develop | [js] jQjuery $ 활용 | hooni | 2013.12.17 | 8876 |
349 | Develop | [js] 순환참조에 의한 메모리 누수 관련 | hooni | 2013.12.17 | 10846 |
348 | Develop | [js] jQuery 치트 시트 | hooni | 2013.12.18 | 36256 |
347 | Develop | [js] 이벤트 전파 3단계 | hooni | 2013.12.18 | 9966 |
346 | System/OS | CentOS 6.5 USB 설치 6 | hooni | 2013.12.18 | 37680 |