const regex = new RegExp("^(/[a-z0–9/_-]*)"); console.log("/newsgjgjgj".replace(regex, "$1/")); console.log("/news?param1=value1".replace(regex, "$1/")); console.log("/news#anchor?param1=value1".replace(regex, "$1/"));