Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's a revised answer based on your comments. I think it does what you need since it deals with missing quotes too.</p> <pre><code>function q(str) { return (/\s/g).test(str) ? str.replace(/^"?(.*?)"?$/, function(str, value) { return '"' + value + '"'; }) : str; } </code></pre> <p><strong>DEMO:</strong> <a href="http://jsbin.com/apeva3/edit" rel="nofollow">http://jsbin.com/apeva3/edit</a></p> <p>Explanation:</p> <p>Pass it a string and it will add the double quotes as needed </p> <ol> <li>If the string has whitespace <code>(/\s/g).test</code></li> <li>Replace everything that is not a starting " or and ending " <ul> <li>replace can take a lambda function, it passes the whole matched string and then each group <code>function(str /*whole string*/, value /* group 1 */)</code></li> <li>the string is replaced by whatever the lambda returns</li> <li>in this case the replace returns whatever isn't in quotes surrounded by quotes</li> </ul></li> </ol> <hr> <p><strong>Old Answer</strong></p> <p>Your whitespace test looks good. For quotes try this:</p> <pre><code>/^(['"])​​​​​​​​​​​​​​.*?\1$/​​​​​​​​​​​​​​​​​ </code></pre> <p>Here is how it works:</p> <ol> <li>If the first character is ' or " match it and remember the value <code>^(['"])</code></li> <li>Now match any number of characters non-greedily <code>.*?</code></li> <li>Then match what was remembered before <code>\1</code></li> <li>And the end of the line <code>$</code></li> </ol>
    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. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    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