Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

What could be wrong in this code?

Posted on 2012-08-29
5
Medium Priority
?
530 Views
Last Modified: 2012-08-29
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);
0
Comment
Question by:dvcphp
  • 4
5 Comments
 
LVL 10

Expert Comment

by:Gregg
ID: 38348184
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.
0
 
LVL 10

Expert Comment

by:Gregg
ID: 38348206
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.
0
 
LVL 10

Accepted Solution

by:
Gregg earned 2000 total points
ID: 38348208
I meant to ask, why do you feel it is wrong?
0
 

Author Comment

by:dvcphp
ID: 38348217
Thanks.very good explanation.I appreciate.will give u full points.
Thanks again.
0
 
LVL 10

Expert Comment

by:Gregg
ID: 38348244
Thanks!
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Suggested Courses

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question