I've found these regex elsewhere:
myString.replace(/^.*\//, '');
myString.replace(/\..*?$/, '');
My question is:
^
and ?$
make any sense here? And my interpretation is that ^
isn't relevant, because we are using.* which means "anything".
The second one means replace from the .
any character in lazy mode till the end of the string. Which is the same as nothing.
So I'd write them:
myString.replace(/.*\//, '');
myString.replace(/\..*/, '');
But I am not 100% sure as my Regex is quite basic. Ideas?
Indeed, in the first regular expression the ^
is not really needed. The effect is the same without it.
However, in the second regular expression there is a difference when you omit the ?
and the $
, because .
does not match newline characters (unless you add s
as modifier).
See the different results:
let myString = `.this is multiline`; console.log(myString.replace(/\..*?$/, '')); console.log(myString.replace(/\..*/, ''));
And my interpretation is that ^ isn't relevant, because we are using.* which means "anything".
Yep, that's the start of the string, but if you're wanting all of the string it has no relevance. Also, go here for a great regex playground.
do ^ and?$ make any sense here?
To answer the second question, for regex /\..*?$/
, the?$ does not make sense with *
, b/cx* means 0 or more of x and x? means 0 or 1 (unless you intentionally wanted 0 or 1 of 0 or more, but doubt that). This is taken directly from the help sheet on regexr.com.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.