eqiproo
eqiproo

Reputation: 1816

How to get two variables from the string?

I have this string: 'foo = bar; this = is; not = necessary'

I would like to get this result: {"name" : "foo", "value" : "bar"}

It is not necessary, what is after the ;

I know, I should use some regexp, but I don't really understand how regexp works.

Thanks is advance,

Upvotes: 0

Views: 70

Answers (2)

Oriol
Oriol

Reputation: 288590

You could use:

var values='foo = bar; this = is; not = necessary'.split('; ')[0].split(' = ');
var obj={"name" : values[0], "value" : values[1]}

This way you do:

  1. 'foo = bar; this = is; not = necessary'.split('; '), which returns ['foo = bar','this = is','not = necessary']
  2. You want just the first element, so 'foo = bar; this = is; not = necessary'.split(';')[0] gives 'foo = bar'
  3. Then you split again: 'foo = bar; this = is; not = necessary'.split(';')[0].split(' = '), which gives ['foo','bar']
  4. And finally you create the object

Upvotes: 3

David Thomas
David Thomas

Reputation: 253456

An approach that uses regular expressions to search in a particular string, for a specified name, which will match the foo = bar portion of the string:

function getNameValuePair(string, needle) {
    if (!string || !needle) {
        return false;
    }
    else {
        var newNeedle = needle + '\\s+\\=\\s+\\w+',
            expr = new RegExp(newNeedle),
            parsed = string.match(expr).join(),
            parts = parsed.split(/\s+\=\s+/),
            returnObj = {'name' : parts[0], 'value' : parts[1]};

        return returnObj;
    }
}

var string = 'foo = bar; this = is; not = necessary',
    nameValueObj = getNameValuePair(string, 'foo');

console.log(nameValueObj);

JS Fiddle demo.

This could, of course, be adapted to more suit your requirements.

As it is, however:

  1. The function checks that both parameters are present and, if not, simply returns false.
  2. Creates a regular expression pattern consisting of the needle ('foo' in this case), followed by one, or more, units of white-space followed by a single = character, followed by one, or more, white-space characters. If the white-space may not always be present, it might be better to change to newNeedle = needle + '\\s*\\=\\s*\\w+' (the + matches 'one-or-more', the * matches 'zero-or-more').
  3. creates a RegExp from that pattern,
  4. tests the given string to see if it matches the pattern and, if it does, stores that match in the parsed variable,
  5. splits the parsed variable with another regular expression, effectively splitting on a pattern of zero-or-more white-space followed by an = followed by zero-or-more white-space, storing the separated parts in the parts array variable,
  6. constructs the object from the parts variable.
  7. Returns that variable.

Updated the above, slightly, because two lines to create one RegExp was just silly:

function getNameValuePair(string, needle) {
    if (!string || !needle) {
        return false;
    }
    else {
        var expr = new RegExp(needle + '\\s+\\=\\s+\\w+'),
            parsed = string.match(expr).join(),
            parts = parsed.split(/\s+\=\s+/),
            returnObj = {'name' : parts[0], 'value' : parts[1]};

        return returnObj;
    }
}

var string = 'foo = bar; this = is; not = necessary',
    nameValueObj = getNameValuePair(string, 'foo');

console.log(nameValueObj);

JS Fiddle demo.

References:

Upvotes: 1

Related Questions