Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If you can 100% guarantee that:</p> <ol> <li>Letter-strings are 2 or 3 characters</li> <li>There are always one or more primes/commas</li> <li>There is never any empty space before, after or in-between the letters and the marks<br> (aside from line-break)</li> </ol> <p>You can use:</p> <pre><code>/^([a-zA-Z]{2,3})('+|,+)$/gm var arr = /^([a-zA-Z]{2,3})('+|,+)$/gm.exec("pok'''"); arr === ["pok'''", "pok", "'''"]; var arr = /^([a-zA-Z]{2,3})('+|,+)$/gm.exec("baf,,,"); arr === ["baf,,,", "baf", ",,,"]; </code></pre> <p>Of course, save yourself some sanity, and save that RegEx as a var.</p> <p>And as a warning, if you haven't dealt with RegEx like this: If a match isn't found -- if you try to match <code>foo',''</code> by mixing marks, or you have 0-1 or 4+ letters, or 0 marks... ...then instead of getting an array back, you'll get <code>null</code>.</p> <p>So you can do this:</p> <pre><code>var reg = /^([a-zA-Z]{2,3})('+|,+)$/gm, string = "foobar'',,''", result_array = reg.exec(string) || [string]; </code></pre> <p>In this case, the result of the exec is null; by putting the || (or) there, we can return an array that has the original string in it, as index-0.</p> <p>Why?</p> <p>Because the result of a successful exec will have 3 slots; <code>[*string*, *letters*, *marks*]</code>. You might be tempted to just read the letters like <code>result_array[1]</code>. But if the match failed and <code>result_array === null</code>, then JavaScript will scream at you for trying <code>null[1]</code>.</p> <p>So returning the array at the end of a failed exec will allow you to get <code>result_array[1] === undefined</code> (ie: there was no match to the pattern, so there are no letters in index-1), rather than a JS error.</p>
 

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