0

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

Thanks a lot in advance

4 Answers 4

3

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.

:)

Sign up to request clarification or add additional context in comments.

Comments

2

In this case you will use

var match = string.match(/(@\w+)/g);

thats it

Comments

1

What about:

var paramsNames = mdxStatement.match(/(@[a-z]+)/gi);

To grab unique params do:

var paramsNames = mdxStatement.match(/(@[a-z]+)/gi).unique();

Comments

1

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/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.