开发者

String parsing with RegExp in Actionscript

I have a string that is similar to a path, but I have tried some regex patterns that are supposed to parse paths and they don't quite work.

Here's the string

f|MyApparel/Templates/Events/

I need the "name parts" between the slashes.

I tried开发者_Python百科 (\w+) but the array came back [0] = "f" and [1] = "f".

I tested the pattern on http://www.gskinner.com/RegExr/ and it seems to work correctly.

Here's the AS code:

var pattern : RegExp = /(\w+)/g;
var hierarchy : Array = pattern.exec(params.category_id);
params.name = hierarchy.pop() as String;


pattern.exec() works like in JavaScript. It resets the lastIndex property every time it finds a match for a global regex, and next time you run it it starts from there.

So it does not return an array of all matches, but only the very next match in the string. Hence you must run it in a loop until it returns null:

var myPattern:RegExp = /(\w+)/g;  
var str:String = "f|MyApparel/Templates/Events/";

var result:Object = myPattern.exec(str);
while (result != null) {
  trace( result.index, "\t", result);
  result = myPattern.exec(str);
}


I don't know between which two slashes you want but try

var hierarchy : Array = params.category_id.split(/[\/|]/);

[\/|] means a slash or a vertical bar.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜