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
번호 | 분류 | 제목 | 글쓴이 | 날짜 | 조회 수 |
---|---|---|---|---|---|
717 | Develop | 논문 실험용 고려대 툴바 ㅎㅎ | hooni | 2013.04.23 | 8686 |
716 | Develop | [ios] 개발 기초 가이드 링크.. | hooni | 2013.04.23 | 31371 |
715 | System/OS | 맥북에서 MAC/윈도우 멀티부팅시 시간 설정 | hooni | 2013.04.23 | 29765 |
714 | Develop | 웹페이지 성능 테스트 툴 설명 | hooni | 2013.04.23 | 27450 |
713 | Develop | [Xcode] 디버깅 옵션 | hooni | 2013.04.23 | 57201 |
712 | Algorithm | 러시아 페인트공 알고리즘에 대해.. | hooni | 2013.04.23 | 22901 |
711 | Develop | [ios] Objective-C 문자열 잘라서 배열(NSArray)에 넣기 | hooni | 2013.04.23 | 29420 |
710 | Develop | [ios] Objective-C 특정 문자 찾아 제거하기 | hooni | 2013.04.23 | 28070 |
709 | Develop | [ios] Objective-C에서 형식이 있는 문자열(Format Strings)에 사용할 수 있는 토큰들(Tokens) | hooni | 2013.04.23 | 18788 |
708 | Develop | [ios] Objective-C 문자열 조작 메서드 | hooni | 2013.04.23 | 26460 |
707 | Develop | [android] [번역] 안드로이드 Android Cloud to Device Messaging(C2DM) | hooni | 2013.04.23 | 20426 |
706 | PPT | [ppt] Equation Solving 발표 자료 (@PWE랩 톡데이 2011.07.26) | hooni | 2013.04.23 | 24674 |