2

I am parsing a webpage and I obtain the following JS function as a string

"translate(737.4170532226562,136.14541625976562)" 

I want to parse the string to obtain the two parameters of the function.

I can parse the string upto the '(' and ',' and ')' to get the arguments - I wanted to know if there is any other method to get the parameters from this string function.

3
  • this is called a production in a grammar, so hopefully this is what it will look like every time you parse. Commented Apr 27, 2016 at 23:01
  • Use a regex like /translate\(([0-9\.]+),([0-9\.])\)/. Or you could make a translate(a,b) function and eval the string, if you trust the source, of course. Commented Apr 27, 2016 at 23:03
  • hey @Dinesh take a look at my answer and let me know what you think. Commented Apr 27, 2016 at 23:13

3 Answers 3

1

You can use regex for this purpose. For example this one: /([\d\.]+),([\d\.]+)/

var str = "translate(737.4170532226562,136.14541625976562)";
var args = /([\d\.]+),([\d\.]+)/.exec(str)
var a1 = args[1], a2 = args[2];

document.write(['First argument: ', a1, '<br> Second argument: ', a2].join(''))

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

Comments

0

This may be overkill. But I was bored. So here is a function name parser. It gets the function name and the arguments.

var program = "translate(737.4170532226562,136.14541625976562)";

function Parser(s)
{
  this.text = s;
  this.length = s.length;
  this.position = 0;
  this.look = '0'
  this.next();
}
Parser.prototype.isName = function() {
    return this.look <= 'z' && this.look >= 'a' || this.look <= '9' && this.look >= '0' || this.look == '.'    
}
Parser.prototype.next = function() {
    this.look = this.text[this.position++];
}
Parser.prototype.getName = function() {
  var name = "";
  while(parser.isName()) {
    name += parser.look;
    parser.next();
  }
  return name;
}

var parser = new Parser(program);
var fname = parser.getName();
var args = [];

if(parser.look == '(') {
  parser.next();
    args.push(parser.getName());
    while(parser.look == ',') {
        parser.next();
        args.push(parser.getName());
    }
} else {
    throw new Error("name must be followed by ()")
}

console.log(fname, args);

2 Comments

Thanks for the prompt answer. I was wondering if we can have a smaller and more compact solution.
take a look at the angular source code and look at their regex for extracting variable names, but beggars shouldn't be choosers :)
0

A littlebit late, but i think this way its much easier to get params or execute a custom function with other function params.

var functionString = "translate(737.4170532226562,136.14541625976562)";
function parser(s){
    let funcName = s.split('(')[0];
    let evalfunc = s.replace(funcName, 'getParams');
    return eval(evalfunc);
}
function getParams(...params){
    return params;
}
console.log(parser(functionString));

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.