Skip to content

Instantly share code, notes, and snippets.

@alumican
Created January 25, 2013 09:59
Show Gist options
  • Save alumican/4633217 to your computer and use it in GitHub Desktop.
Save alumican/4633217 to your computer and use it in GitHub Desktop.
文字列の中から指定した正規表現にマッチするものを見つける
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(", ") + "]");
@alumican
Copy link
Author

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]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment