Created
August 10, 2014 15:16
-
-
Save jikeytang/85c1fd4738e31adffc7e to your computer and use it in GitHub Desktop.
[ Javascript ] - 20140811-题目1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
如何用非正则的方式找出一个字符串中出现的数字序列。 | |
比如:var str = 'asdfasdf213sdd123aaa123aad123fdssaa'; -> arr = [213, 123, 123, 123] | |
PS: | |
1. 回复时注意加上下面这句话,才会有语法高亮或格式缩进。 | |
```javascript | |
// you code | |
``` | |
2. 粘贴代码时请使用shift+tab,缩进前面的空白。 |
lixianwie
commented
Aug 11, 2014
// 使用正则
var str = 'asdfasdf213sdd123aaa123aad123fdssaa';
var re = /\d+/g;
console.log( str.match(re));
// 不适用正则
var len = str.length ;
var arr = [];
var temp = '';
for(var i = 0 ; i < len; i ++){
if( str.charAt(i) < 10 ){
temp += str.charAt(i);
}else{
if( temp != '' ){
arr.push(temp);
}
temp = '';
}
}
console.log( arr )
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment