user1700534
user1700534

Reputation: 13

Get value from string parameter of JavaScript function

In the following standard function you can put in multiple arguments for the second and third parameter.

var retval = window.showModalDialog(dialog, varArgIn, varOptions);

The varOptions parameter can be filled with:

'dialogHeight:580px;dialogWidth:700px;center=yes;scroll=no', 

How do I get the only the value that's set at dialogHeight? So I can re-use only that value somewhere else.

Upvotes: 1

Views: 295

Answers (6)

Minko Gechev
Minko Gechev

Reputation: 25672

You can use simple regular expression:

'dialogHeight:580px;dialogWidth:700px;center=yes;scroll=no'.match(/dialogHeight:\w+/)[0]; //dialogHeight:580px

In your case, probably you'll need: varOptions.match(/dialogHeight:\w+/)[0];.

If you need only the value, you can use:

varOptions.match(/dialogHeight:(\w+)/)[1];.

As founddrama suggested, if dialogHeight is not a substring of varOptions the code will produce an error. Better way of doing the above thing is:

var match = varOptions.match(/dialogHeight:(\w+)/);
if (match && match.length >= 1) {
    console.log("The value is: " + match[1]);
}

Upvotes: 3

Jonny Buchanan
Jonny Buchanan

Reputation: 62793

var parseDialogOptions = function() {
  var optionDivider = /\s*;\s*/
    , valueDivider = /\s*[:=]\s*/
    , whiteSpace = /^\s+|\s+$/g

  return function(optionString) {
    var options = optionString.split(optionDivider)
      , obj = {}
    for (var i = 0, l = options.length; i < l; i++) {
      var parts = options[i].split(valueDivider)
      if (parts.length == 2) {
        obj[parts[0].replace(whiteSpace, '')] = parts[1].replace(whiteSpace, '')
      }
    }
    return obj
  }
}()

Usage:

>>> parseDialogOptions('dialogHeight:580px;dialogWidth : 700px; center=yes ; scroll=no ; ')
Object { dialogHeight="580px", dialogWidth="700px", center="yes", scroll="no" }

Upvotes: 0

Marcin Buciora
Marcin Buciora

Reputation: 449

Use regular expressions in following way:

var s= 'dialogHeight:580px;dialogWidth:700px;center=yes;scroll=no';
alert(s.match(/dialogHeight:[\w]+\;/)[0].match(/:[\w]+;/)[0].replace(':',''));

Upvotes: 1

Tom Pietrosanti
Tom Pietrosanti

Reputation: 4294

Assuming you don't already have the values, as in @Henrik Peinar's answer, you can parse the value from the string, (as long as it follows the conventions in your example).

Here are a couple ways you could do it:

1) Format the string to JSON notation, convert to object, access values

var objOptions = JSON.parse("{" + varOptions.replace(/;/g,",").replace(/=/g,":").replace(/(\w+)/g,'"$1"') + "}");

console.log(objOptions.dialogHeight);

2) Parse out only the value you're looking for using RegEx (my example is prone to errors, so I would do it a bit differently in production. This is just a quick example.)

varOptions.match(/dialogHeight:(\w+);/)[1]

3) Use split to get an array of the name-value pairs, then loop and split those results, testing the 'name' piece until you find the one you want. (I'm running out of time, so I've left off this example)

Upvotes: 0

Stuart
Stuart

Reputation: 9858

If you are liable to need any of the other values it will be useful to have a function that converts the string of options into a dictionary-style object.

function getOptions(s) {
  var r = {}, kv;
  s = s.split(';');
  for (var i = 0; i < s.length; i++) {
    kv = s[i].split(s[i].indexOf(':') > -1 ? ':' : '=');
    r[kv[0]] = kv[1];
  }
  return r;
}
var opts = 'dialogHeight:580px;dialogWidth:700px;center=yes;scroll=no';
var optsObj = getOptions(opts);
console.log(optsObj.dialogHeight);

Upvotes: 1

Henrik Peinar
Henrik Peinar

Reputation: 2201

Don't understand your problem. YOU fill the varOptions, don't you? Just create new variable for each option and re-use them all you want.

So something like

var dialogHeight = 580;
var dialogWidth = 700;
var center = 'yes';
var scroll = 'no';

var options = 'dialogHeight:'+ dialogHeight +'px;dialogWidth:'+ dialogWidth '+x;center='+ center +';scroll='+ scroll;
var retval = window.showModalDialog(dialog, varArgIn, options);
alert('Reusing dialog height vairable here:'+ dialogHeight);

If you want to get dialogHeight out from the string you gave, you could do the following:

var input = 'dialogHeight:580px;dialogWidth:700px;center=yes;scroll=no';
var dialogHeight = input.match(/(?!dialogHeight:)[0-9]*(?=p)/ ,input);
alert(dialogHeight);

http://jsfiddle.net/E67RF/ <- working fiddle with this code

Upvotes: 1

Related Questions