[英]Help, Javascript regular expression
I'm always fighting with regular expression, some help would be appreciated.我一直在与正则表达式作斗争,我们将不胜感激。
What is the best way to get all parameters (words starting with @) in strings like:在字符串中获取所有参数(以@开头的单词)的最佳方法是:
statement : "SELECT @Measure on 0, TOPCOUNT(@Hierarchy.levels(1).member) where @Measure"
This expression is not working correctly:此表达式无法正常工作:
var paramsNames = mdxStatement.match(/(^|\s|-)+@(\w+)/g);
The expected result is: @Measure,@Hierarchy预期结果是:@Measure,@Hierarchy
Thanks a lot in advance非常感谢提前
Use this:用这个:
"SELECT @Measure on 0, TOPCOUNT(@Hierarchy.levels(1).member) where @Measure".match(/(@\w+)/g);
And remove the last item, if you do not need it.并删除最后一项,如果您不需要它。
:) :)
In this case you will use在这种情况下,您将使用
var match = string.match(/(@\w+)/g);
thats it而已
What about:关于什么:
var paramsNames = mdxStatement.match(/(@[a-z]+)/gi);
To grab unique params do:要获取独特的参数,请执行以下操作:
var paramsNames = mdxStatement.match(/(@[a-z]+)/gi).unique();
I believe我相信
/@\b.+?\b/g
should do what you want应该做你想做的
Array.prototype.unique = function() {
var o = {}, i, l = this.length, r = [];
for(i=0; i<l;i+=1) o[this[i]] = this[i];
for(i in o) r.push(o[i]);
return r;
};
var string = "SELECT @Measure on 0, TOPCOUNT(@Hierarchy.levels(1).member) where @Measure";
var pattern = /@\b.+?\b/g;
var params = string.match(pattern).unique();
demo http://jsfiddle.net/gaby/se7Np/1/演示http://jsfiddle.net/gaby/se7Np/1/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.