<p>Use</p>
<pre><code>/^(.*?)(v\d+)?$/
</code></pre>
<p>See <a href="https://regex101.com/r/quRVSn/1" rel="nofollow noreferrer">proof</a>.</p>
<p><strong>EXPLANATION</strong></p>
<pre><code>NODE                     EXPLANATION
--------------------------------------------------------------------------------
  ^                        the beginning of the string
--------------------------------------------------------------------------------
  (                        group and capture to \1:
--------------------------------------------------------------------------------
    .*?                      any character except \n (0 or more times
                             (matching the least amount possible))
--------------------------------------------------------------------------------
  )                        end of \1
--------------------------------------------------------------------------------
  (                        group and capture to \2 (optional
                           (matching the most amount possible)):
--------------------------------------------------------------------------------
    v                        'v'
--------------------------------------------------------------------------------
    \d+                      digits (0-9) (1 or more times (matching
                             the most amount possible))
--------------------------------------------------------------------------------
  )?                       end of \2 (NOTE: because you are using a
                           quantifier on this capture, only the LAST
                           repetition of the captured pattern will be
                           stored in \2)
--------------------------------------------------------------------------------
  $                        before an optional \n, and the end of the
                           string
</code></pre>
<p>JavaScript:</p>
<p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
<div class="snippet-code">
<pre class="snippet-code-js lang-js prettyprint-override"><code>let url = 'https://arxiv.org/abs/2004.10934v3'
let regURL = /^(.*?)(v\d+)?$/;
let [_, url_f, version] = regURL.exec(url);
console.log(url_f);</code></pre>
</div>
</div>
</p>
Get segment of string without specific suffix with JS regex
<p>I have a url, it can either be</p>
<pre><code>https://arxiv.org/abs/2004.10934
</code></pre>
<p>or with a suffix v[0-9]</p>
<pre><code>https://arxiv.org/abs/2004.10934v3
</code></pre>
<p>I want to filtered out the v[0-9] part with regex.</p>
<p>That is, getting 'https://arxiv.org/abs/2004.10934', no matter which type of url us given.</p>
<p>Below is what I have for now, it works but seems hacky...</p>
<pre><code>let url = 'https://arxiv.org/abs/2004.10934v3'
let regURL = /(.*(?&lt;!v[0-9])(?&lt;!v))/g;
let url_f = regURL.exec(url)[0];
</code></pre>
<p>Is there a better regex pattern for this?</p>