Link to home
Start Free TrialLog in
Avatar of dvcphp
dvcphp

asked on

What could be wrong in this code?

Hello Experts,
Please let me know what is wrong with this code.I dont get the output in the console.
 
var str=".m.stubhub.com";
console.log(str.replace(/\.[^.]+/,""));
console.log(str);
Avatar of Gregg
Gregg
Flag of United States of America image

In your regex: /\.[^.]+/

If i understand correctly, this expression will return a match that "is a period followed by a any one or more characters that is not a period". The result that is returned will be replaced by nothing (the double quotes).

In ".m.stubhub.com" the match is the first period followed by m. That is where it stops because a period followed by an m followed by a period does not meet the regex criteria.
Looking closer that the regex:

/.../ The outer forward slashes says im a regular expression.

\. A dot is metacharacter outside a character class. In your regex you literally mean a period character. So you escape the dot metacharacter with a slash. So the \. literally means a period.

[^.] This is a negated character class. This is used to match any character that is not a period. The period inside a [] is a literal period character. It is not a meta character. So you are saying in this regex that you literally want any character that is not a period.

+ The negated character class matches any character. The plus means you require at least one match; and unlimited matches are allowed. So one or more matches that are not a period.
ASKER CERTIFIED SOLUTION
Avatar of Gregg
Gregg
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dvcphp
dvcphp

ASKER

Thanks.very good explanation.I appreciate.will give u full points.
Thanks again.
Thanks!