Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ilex01's avatar

window.location.href Regex URL

What is the regex for this?

if(window.location.href === "https://www.[*]/g/[*]/recent") // 1. 2. 3. 4. (see below)
{
	$(function() {
		$(document).scrollTop( $("#anchor2").offset().top );
	});
}

// 1. window.location.href = http://[*]/g/[*]/recent
// 2. window.location.href = https://[*]/g/[*]/recent
// 3. window.location.href = http://www.[*]/g/[*]/recent
// 4. window.location.href = https://www.[*]/g/[*]/recent
0 likes
5 replies
JabatoForever's avatar

@ilex01 the $ character at the end of the regex checks that the match must be at the end of the string. In this case, the regex is checking if /recent is the last thing in the URL if so the page will be scrolled to the element with id #anchor2 ok i tested it is not valid

window.location.href.match(/\/recent$/)

change it to this u should match recent at the and of a string

Please or to participate in this conversation.