Skip to content

Instantly share code, notes, and snippets.

@NKid
Last active August 21, 2020 08:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NKid/e37cfbfaa47f22c71f27 to your computer and use it in GitHub Desktop.
Save NKid/e37cfbfaa47f22c71f27 to your computer and use it in GitHub Desktop.
遍歷字串每個字元
//Method 1
var str = "132ada5d6g3j";
var match = str.match(/\d/g, str);
var arr = [];
var j;
for(var i = 0, j = match.length; i < j; i++) {
arr.push(match[i]);
}
console.log(arr);
//Method 2
var str = "132ada5d6g3j";
var arr = [];
str.replace(/\d/g, function(n) {
arr.push(n); //arr.push(arguments[0]));
});
console.log(arr);
//Method 3
var str = "132ada5d6g3j";
var arr = str.replace(/\D/g, '').split('');
console.log(arr);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment