title: Using regex, Conditionally replace with $1 or empty string
body: <p>I want to create a function in javascript which accepts two arguments : a regex object and a string.<br>
  The regex can have at most one capturing group.<br>
  If there is a capturing group, it shall return $1, otherwise the remaining string;</p>
  
  <p>So <code>myfunc("hello123", /\d+/)</code> shall return hello and<br>
  <code>myfunc("hello123", /\w+(\d+)/)</code> shall return 123</p>
  
  <p>How to identify if there's a capturing group or not?</p>