Created
January 25, 2013 09:59
-
-
Save alumican/4633217 to your computer and use it in GitHub Desktop.
文字列の中から指定した正規表現にマッチするものを見つける
This file contains 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
function find(source:String, pattern:RegExp, callback:Function = null):Array { | |
var indices:Array = new Array(); | |
var result:Object; | |
var i:int = -1; | |
while (result = pattern.exec(source)) { | |
++i; | |
if (callback != null) callback.call(null, i, result); | |
indices.push(result.index); | |
if (!pattern.global) break; | |
} | |
return indices; | |
} | |
/* TEST CODE*/ | |
function callback(index:int, result:Object):void { | |
trace("index : " + index); | |
trace(" result.index = " + result.index); | |
trace(" result.result[0] = " + result[0]); | |
trace(""); | |
} | |
var indices:Array = find(",*あいうえおABCDE漢字123", /[\u0020-\u007e]+/g, callback); | |
trace("indices = [" + indices.join(", ") + "]"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
index : 0
result.index = 0
result.result[0] = ,*
index : 1
result.index = 7
result.result[0] = ABCDE
index : 2
result.index = 14
result.result[0] = 123
indices = [0, 7, 14]