My code is as follows:
var regex = new RegExp ('(.*/*)');
console.log(regex);
I think the result is:
/(.*/*)/
But the actual result is:
/(.*\/*)/
Could someone please explain this for me?
The leading and trailing forward slashes are just how JavaScript represents a regex in string form and as a literal. The mean the same thing:
var regex = new RegExp ('(.*/*)');
is the same as
var regex = /(.*\/*)/;
It is important to escape the middle / otherwise it would interpret it as the end of the literal.
If you expect the last asterisk * to be literal you need to escape it, otherwise it's a quantifier in regex meaning "match between zero and unlimited times". It's also recommened to escape the forward slash /, even if it might work without doing so.
(.*\/\*)
If you want to match any string /* any string then use:
(.*\/\*.*)