I like to code parameters to my Javascript functions within strings
that look like UNIX command parameters. For instance I might say
myfunc("-center -w:100% -nocache");
I've written a function to extract the value of any one flag or
keyword:value pair, but it's not working. To continue with the example,
I call
get_value(values_passed, "-w:80%");
which means that 'values_passed' is the string that was passed to me,
'-w:' is the keyword I'm interested in and '80%' is its default value.
Here is the function I wrote:
function get_value(parms, key_and_val)
{
var colon = key_and_val.indexOf(':');
var key = key_and_val.substr(0, colon);
var val = key_and_val.substr(colon + 1);
var regex = new RegExp(key + ":(.*)[ $]"); // IS THIS CORRECT?
var rslt = regex.exec(parms);
var result;
if (rslt == null)
{
result = val; // Use default value
}
else
{
result = rslt[0];
}
return result;
}
'rslt' is always null. I've even tried using match() instead of exec():
var rslt = parms.match(regexp);
and that results in 'null' too. Can anyone think of anything I can try
to learn out what's wrong here? Does my regexp violate some syntax rule
I'm unaware of, for instance? Thanks very, very much.
Dean Hannotte
http://www.hannotte.net