Regular expression for substring


can someone please tell me what will be the regular expression for this line:

var id = $(this).attr("href").substring( $(this).attr("href").lastIndexOf('#'), $(this).attr("href").length);

Basically it gets the string after # in href attribute of clicked anchor tag.


No offence, but regex won’t improve that code, only make it slower.
indexOf and .length are way faster than regex.

If anything I would make it $(‘html,body’).stop(true).animate( … );

to make sure previous animations of body and html stop immediately and start the new animation right away, to avoid a long queue of scrolling up and down and up and down and …


Thanks :slight_smile:

I tried this:

var id = $(this).attr("href").match(#(\\b[^\\"]*));

But it’s giving me error :frowning:

Actually i have created following function to scroll to the named anchors, this function is working fine, i am just trying to improve and shorten the code.

$(document).ready(function() {
			var id = $(this).attr("href").substring( $(this).attr("href").lastIndexOf('#'), $(this).attr("href").length);
			$('html,body').animate({scrollTop: $(id).offset().top},'slow');
			return false;

Any other suggestions for improvement are also welcome.



you can get anything in href after # with next regular expression #(\b[^\"]*)

I hope this helps