0

I'm trying to parse apart a string like this with javascript:

var mystr = "param1('stringValue'), param2(IntegerValue IntegerValue)";

Where in the end I need to get each of the param names (param1 and param2) and values (either one string value, or two integer values).

However, the original string could have any number of parameters, such as:

"param1('stringValue')"

"param1('stringValue'), param2(IntegerValue IntegerValue), param2(IntegerValue IntegerValue)"

Requirements:

  1. Between each parameter is a comma, and values are within the parentheses.
  2. Within the parentheses could either be one string value, or two integers with a space in between.
  3. There could be any number of parameters (but at least 1)
4
  • Maybe, you can adopt this idea: stackoverflow.com/questions/9772569/simple-advanced-search/… Commented Mar 29, 2012 at 18:48
  • Why regex? Split after , then for each value replace ) with "" and split after ( then you can have an array where on every position you have the name of the param ( param1, param2) and the actual parameter. Or you could format your string to be a JSON. Commented Mar 29, 2012 at 18:56
  • Split is ok except cases, when string parametr has a ,. Commented Mar 29, 2012 at 18:59
  • The values wouldn't have a (,) in them, so maybe doing some split magic would be better. I was thinking this may be more suited for regex, but maybe not. Commented Mar 29, 2012 at 19:08

2 Answers 2

2
var str = "param1('stringValue'), param2(1 2), param2(34 555)";
var re = /(\w+)\((?:'([^']+)'|(\d+) (\d+))\)/g;
var a = [], match, value;
while(match = re.exec(str)) {
    if (match[2] != null) value = match[2];
    else value = [parseInt(match[3]), parseInt(match[4])];
    a.push([match[1], value]);
}
a; // [["param1", "stringValue"], ["param2", [1, 2]], ["param2", [34, 555]]]

Strings should not have ' symbols in them. If you want completely foolproof solution, you need to abandon regexs and use some parser.

Parsing escaped string would be possible with something like [^']|(<?\\(?:\\\\)*)', but regexes in js doesn't support look-behinds and AFAIR variable-length look-behinds are not supported at all.

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

1 Comment

How would I get the param names, such as 'param1', 'param2', 'param3'?
1
function parse( str ) {

    var rparse = /([a-zA-Z0-9$_]+)\(|'([^']*)'|(\d+ \d+)/g,     
        cur, r = [];

        while( ( cur = rparse.exec( str ) ) ) {

            if( cur[1] ) {
                r.push( {name: cur[1] } );
            }
            else if( cur[2] ) {
                r[r.length-1].values = [cur[2]];

            }
            else if( cur[3] ) {
                r[r.length-1].values = cur[3].split(" ").map( Number );
            }

        }

        return r;

}

http://jsfiddle.net/xmajs/

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.