Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Four options / thoughts / suggestions:</p> <h3>1. Use JSON</h3> <p>If you're in control of the source format, I'd recommend using <a href="http://json.org" rel="nofollow">JSON</a> rather than rolling your own. Details on that page. JSON is now part of the <a href="http://www.ecma-international.org/publications/standards/Ecma-262.htm" rel="nofollow">ECMAScript (JavaScript) standard</a> with standard methods for creating JSON strings from object graphs and vice-versa. With your example:</p> <pre><code>exampleVariable = { name: "string with spaces", cake:lie }; variableName = "variableValue,NoSpacesThough"; portal = "The Cake Is A Lie"; </code></pre> <p>here's what the JSON equivalent would look like:</p> <pre><code>{ "exampleVariable": { name: "string with spaces", cake:lie }, "variableName": "variableValue,NoSpacesThough", "portal": "The Cake Is A Lie" } </code></pre> <p>As you can see, the only differences are:</p> <ol> <li>You wrap the entire thing in curly braces (<code>{}</code>).</li> <li>You put the "variable" names (property names) in double quotes.</li> <li>You use a colon rather than an equal sign after the property name.</li> <li>You use a comma rather than a semicolon to separate properties (just as in the object literal you have on your <code>exampleVariable</code> line).</li> <li>You ensure that any string values use double, rather than single, quotes (JavaScript allows either; JSON is more restrictive). Your example uses double quotes, but I mention it just in case...</li> </ol> <h3>2. Pre-process it into JSON with regular expressions</h3> <p>If you're not in control of the source format, but it's <em>exactly</em> as you've shown, you could reformat it as JSON fairly easily via regular expressions, and then deserialize it with the JSON stuff. But if the format is more complicated than you've quoted, that starts getting hairy very quickly.</p> <p>Here's an example (<a href="http://jsbin.com/uqexil" rel="nofollow">live copy</a>) of transforming what you've quoted to JSON:</p> <pre><code>function transformToJSON(str) { var rexSplit = /\r?\n/g, rexTransform = /^\s*([a-zA-Z0-9_]+)\s*=\s*(.+);\s*$/g, rexAllWhite = /\s+/g, lines, index, line; lines = str.split(rexSplit); index = 0; while (index &lt; lines.length) { line = lines[index]; if (line.replace(rexAllWhite, '').length === 0) { // Blank line, remove it lines.splice(index, 1); } else { // Transform it lines[index] = line.replace(rexTransform, '"$1": $2'); ++index; } } result = "{\n" + lines.join(",\n") + "\n}"; return result; } </code></pre> <p>...but <strong>beware</strong> as, again, that relies on the format being exactly as you showed, and in particular it relies on each value being on a single line and any string values being in double quotes (a requirement of JSON). You'll probably need to handle complexities the above doesn't handle, but you can't do it with things like your first line <code>var s = (document.getElementById("gv").innerHTML).split(';');</code>, which will break lines on <code>;</code> regardless of whether the <code>;</code> is within quotes...</p> <h3>3. <em>Actually</em> parse it by modifying a JSON parser to support your format</h3> <p>If you can't change the format, and it's less precise than the examples you've quoted, you'll have to get into actual parsing; there are no shortcuts (well, no <em>reliable</em> ones). Actually parsing JavaScript literals (I'm assuming there are not <em>expressions</em> in your data, other than the assignment expression of course) isn't that bad. You could probably take a JSON parser and modify it to your needs, since it will already have nearly all the logic for literals. There are two on Crockford's github page (Crockford being the inventer of JSON), <a href="https://github.com/douglascrockford/JSON-js/blob/master/json_parse.js" rel="nofollow">one using recursive descent</a> and <a href="https://github.com/douglascrockford/JSON-js/blob/master/json_parse_state.js" rel="nofollow">another using a state machine</a>. Take your choice and start hacking.</p> <h3>4. The evil <code>eval</code></h3> <p>I suppose I should mention <code>eval</code> here, although I don't recommend you use it. <code>eval</code> runs arbitrary JavaScript code from a string. But because it runs <em>any</em> code you give it, it's not a good choice for deserializing things like this, and any free variables (like the ones you've quoted) would end up being globals. Really very ugly, I mostly mention it in order to say: Don't use it. :-)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload