0

I am trying to split the following string:

delete(value1,value2);

I want to get the values and save them in a array:

var values = [value1,value2]
python38291
  • 45
  • 1
  • 6
  • 1
    Feels like an XY problem here. Why do you have a function declaration that is a string? And you can’t access the live values of the arguments in a string since they’re most likely only available at runtime. – Terry Nov 24 '19 at 18:21
  • @Terry I have updated it to not cause confusion. The important thing is to get the values of the parenthesis. – python38291 Nov 24 '19 at 18:22
  • 1
    Duplicate of [How to get function parameter names/values dynamically?](https://stackoverflow.com/questions/1007981/how-to-get-function-parameter-names-values-dynamically) – Stefan Steiger Nov 24 '19 at 18:34

3 Answers3

1

A regex would do the trick:

/\((.*)\)/g.exec('delete(value1,value2);')[1].split(',')

This captures anything between parentheses, which you can then split again.

Brother Woodrow
  • 6,092
  • 3
  • 18
  • 20
0

You can split the string using split function


var str = "delete(value1,value2)";
var stringArray = str.split("(");
var values=stringArray[1].split(",");

use Replace method to finally delte ")" from second string in array

values[1]=values[1].replace(")","");

0

How about:

var STRIP_COMMENTS = /((\/\/.*$)|(\/\*[\s\S]*?\*\/))/mg;
var ARGUMENT_NAMES = /([^\s,]+)/g;


function getParamNames(fnStr) {
  var fnStr = fnStr.replace(STRIP_COMMENTS, '');
  var result = fnStr.slice(fnStr.indexOf('(')+1, fnStr.indexOf(')')).match(ARGUMENT_NAMES);
  if(result === null)
     result = [];
  return result;
}


var str = "delete(value1,value2)";
getParamNames(str);
Stefan Steiger
  • 78,642
  • 66
  • 377
  • 442