I want to create a function in javascript which accepts two arguments : a regex object and a string.
The regex can have at most one capturing group.
If there is a capturing group, it shall return $1, otherwise the remaining string;
So myfunc("hello123", /\d+/) shall return hello and
myfunc("hello123", /\w+(\d+)/) shall return 123
How to identify if there's a capturing group or not?
myfunc("hello123", /\d+/) shall return hello..you are replacing the second argument with nothing and returning the string. In your second example:myfunc("hello123", /\w+(\d+)/) shall return 123you are matching and returning match[1]. There has to be a uniform behaviour to build a function on.myfunc("hello123", /\d+/), no matching group, so the string matching the given pattern /\d+/ shall be removed. second examplemyfunc("hello123", /\w+(\d+)/), there's a matching group, so I need the part that matched!