Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>New answer</strong></p> <p>Regarding your example I'd say it is really easier to split the string and iterate over it:</p> <pre><code>function filter(str, delim, test) { var parts = str.split(delim), result = []; for(var i = 0, len = parts.length; i &lt; len; i++) { if(test(parts[i])) result.push(parts[i]); } return result.join(delim); } str = filter(str, ', ', function(s) { s = s.toLowerCase(); return s === 'bair hugger' || s === 'fluid warmer'; }); </code></pre> <p>Otherwise, your expression becomes something like this:</p> <pre><code>new RegExp("(^|, )(?!(?:Bair Hugger|Fluid Warmer)(?:$|, )).+?(, |$)", "i"); </code></pre> <p>and you have to use a callback for the replacement to decide whether to remove the preceding <code>,</code> or trailing <code>,</code> or not:</p> <pre><code>str = str.replace(re_comma, function(str, pre, tail) { return pre &amp;&amp; tail ? tail : '';// middle of the string, leave one }); </code></pre> <p>The intention of this code is less clear. Maybe there is a simpler expression, but I think filtering the array is still cleaner.</p> <hr> <p><strong>Old answer:</strong> (doesn't solve the problem at hand but provides information regarding regular expressions).</p> <p><code>[]</code> denotes a <em>character class</em> and will only match <strong>one</strong> character out of the ones you provided. <code>[^Bair Hugger|Fluid Warmer]</code> is the same as <code>[^Bair Huge|FldWm]</code>.</p> <p>You could use a negative lookahead:</p> <pre><code>new RegExp("^(?!(Bair Hugger|Fluid Warmer), ).+?, $", "i"); </code></pre> <p>Note that you have to use <code>\\</code> inside a string to produce one <code>\</code>. Otherwise, <code>"\w"</code> becomes <code>w</code> and is not a special character sequence anymore.You also have to anchor the expression. </p> <p><strong>Update:</strong> As you mentioned you want to match any string before the comma, I decided to use <code>.</code> instead of <code>\w</code>, to match any character.</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