Remove the hash / pound symbol from the beginning

I came across this article that talked about AJAX and jQuery and I reached this line

hash = hash.replace(/^.*#/, ''); 

where hash has the form # page1, # page2, etc.

Now I don’t understand why

 hash = hash.replace(/^#/, ''); 

will not be enough. From Mozilla Docs ,

^ Corresponds to the beginning of input. If the multi-line flag is set to true, it also matches immediately after the line break character.

So, with this caret, I should be able to match the hash value, what is the author trying to do?

+4
source share
3 answers

Your suggested regular expression will only match the hash character at the beginning of the line, and nowhere else. The first regular expression you placed will match all the characters of the first character in the string.

+3
source

.* means "any number of any characters", so it seems that the author believes that something can precede the octotorpus. You are right in believing that your simpler template will work too.

+1
source

The first form will delete the hash and everything that will lead to it. So if this is really a hash place, it doesn't matter. If you have the full URL, you will leave only the part after #.

0
source

All Articles