Note that there are some explanatory texts on larger screens.

plurals
  1. PORegular expression to remove one parameter from query string
    text
    copied!<p>I'm looking for a regular expression to remove a single parameter from a query string, and I want to do it in a single regular expression if possible.</p> <p>Say I want to remove the <code>foo</code> parameter. Right now I use this:</p> <pre><code>/&amp;?foo\=[^&amp;]+/ </code></pre> <p>That works as long as <code>foo</code> is not the first parameter in the query string. If it is, then my new query string starts with an ampersand. (For example, "<code>foo=123&amp;bar=456</code>" gives a result of "<code>&amp;bar=456</code>".) Right now, I'm just checking after the regex if the query string starts with ampersand, and chopping it off if it does.</p> <p>Example edge cases:</p> <pre><code>Input | Expected Output -------------------------+-------------------- foo=123 | (empty string) foo=123&amp;bar=456 | bar=456 bar=456&amp;foo=123 | bar=456 abc=789&amp;foo=123&amp;bar=456 | abc=789&amp;bar=456 </code></pre> <hr> <h3>Edit</h3> <p>OK as pointed out in comments there are there are way more edge cases than I originally considered. I got the following regex to work with all of them:</p> <pre><code>/&amp;foo(\=[^&amp;]*)?(?=&amp;|$)|^foo(\=[^&amp;]*)?(&amp;|$)/ </code></pre> <p>This is modified from <a href="https://stackoverflow.com/questions/1842681/regular-expression-to-remove-one-parameter-from-query-string/1842787#1842787">Mark Byers's answer</a>, which is why I'm accepting that one, but Roger Pate's input helped a lot too.</p> <p>Here is the full suite of test cases I'm using, and a Javascript snippet which tests them:</p> <p><div class="snippet" data-lang="js" data-hide="false" data-console="false" data-babel="false"> <div class="snippet-code"> <pre class="snippet-code-js lang-js prettyprint-override"><code>$(function() { var regex = /&amp;foo(\=[^&amp;]*)?(?=&amp;|$)|^foo(\=[^&amp;]*)?(&amp;|$)/; var escapeHtml = function (str) { var map = { '&amp;': '&amp;amp;', '&lt;': '&amp;lt;', '&gt;': '&amp;gt;', '"': '&amp;quot;', "'": '&amp;#039;' }; return str.replace(/[&amp;&lt;&gt;"']/g, function(m) { return map[m]; }); }; //test cases var tests = [ 'foo' , 'foo&amp;bar=456' , 'bar=456&amp;foo' , 'abc=789&amp;foo&amp;bar=456' ,'foo=' , 'foo=&amp;bar=456' , 'bar=456&amp;foo=' , 'abc=789&amp;foo=&amp;bar=456' ,'foo=123' , 'foo=123&amp;bar=456' , 'bar=456&amp;foo=123' , 'abc=789&amp;foo=123&amp;bar=456' ,'xfoo' , 'xfoo&amp;bar=456' , 'bar=456&amp;xfoo' , 'abc=789&amp;xfoo&amp;bar=456' ,'xfoo=' , 'xfoo=&amp;bar=456' , 'bar=456&amp;xfoo=' , 'abc=789&amp;xfoo=&amp;bar=456' ,'xfoo=123', 'xfoo=123&amp;bar=456', 'bar=456&amp;xfoo=123', 'abc=789&amp;xfoo=123&amp;bar=456' ,'foox' , 'foox&amp;bar=456' , 'bar=456&amp;foox' , 'abc=789&amp;foox&amp;bar=456' ,'foox=' , 'foox=&amp;bar=456' , 'bar=456&amp;foox=' , 'abc=789&amp;foox=&amp;bar=456' ,'foox=123', 'foox=123&amp;bar=456', 'bar=456&amp;foox=123', 'abc=789&amp;foox=123&amp;bar=456' ]; //expected results var expected = [ '' , 'bar=456' , 'bar=456' , 'abc=789&amp;bar=456' ,'' , 'bar=456' , 'bar=456' , 'abc=789&amp;bar=456' ,'' , 'bar=456' , 'bar=456' , 'abc=789&amp;bar=456' ,'xfoo' , 'xfoo&amp;bar=456' , 'bar=456&amp;xfoo' , 'abc=789&amp;xfoo&amp;bar=456' ,'xfoo=' , 'xfoo=&amp;bar=456' , 'bar=456&amp;xfoo=' , 'abc=789&amp;xfoo=&amp;bar=456' ,'xfoo=123', 'xfoo=123&amp;bar=456', 'bar=456&amp;xfoo=123', 'abc=789&amp;xfoo=123&amp;bar=456' ,'foox' , 'foox&amp;bar=456' , 'bar=456&amp;foox' , 'abc=789&amp;foox&amp;bar=456' ,'foox=' , 'foox=&amp;bar=456' , 'bar=456&amp;foox=' , 'abc=789&amp;foox=&amp;bar=456' ,'foox=123', 'foox=123&amp;bar=456', 'bar=456&amp;foox=123', 'abc=789&amp;foox=123&amp;bar=456' ]; for(var i = 0; i &lt; tests.length; i++) { var output = tests[i].replace(regex, ''); var success = (output == expected[i]); $('#output').append( '&lt;tr class="' + (success ? 'passed' : 'failed') + '"&gt;' + '&lt;td&gt;' + (success ? 'PASS' : 'FAIL') + '&lt;/td&gt;' + '&lt;td&gt;' + escapeHtml(tests[i]) + '&lt;/td&gt;' + '&lt;td&gt;' + escapeHtml(output) + '&lt;/td&gt;' + '&lt;td&gt;' + escapeHtml(expected[i]) + '&lt;/td&gt;' + '&lt;/tr&gt;' ); } });</code></pre> <pre class="snippet-code-css lang-css prettyprint-override"><code>#output { border-collapse: collapse; } #output tr.passed { background-color: #af8; } #output tr.failed { background-color: #fc8; } #output td, #output th { border: 1px solid black; padding: 2px; }</code></pre> <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"&gt;&lt;/script&gt; &lt;table id="output"&gt; &lt;tr&gt; &lt;th&gt;Succ?&lt;/th&gt; &lt;th&gt;Input&lt;/th&gt; &lt;th&gt;Output&lt;/th&gt; &lt;th&gt;Expected&lt;/th&gt; &lt;/tr&gt; &lt;/table&gt;</code></pre> </div> </div> </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