Search code examples
phpjquerysubstrstrpos

Translate PHP String Functions to Javascript / jQuery


This is the PHP code I'm trying to translate into Javascript / jQuery. Any ideas?

$root_folder = 'es';
$url = "http://domainNamehere.com/event/test-event-in-the-future/";

$p = strpos($url, '/', 8);
$url_new = sprintf('%s/%s/%s', substr($url, 0, $p), $root_folder, substr($url, $p+1));

Solution

  • var root_folder = "es",
        url = "http://domainNamehere.com/event/test-event-in-the-future/",
        url_new = "",
        path_finder = /^(((http)s?:)?\/\/[^\/]+\/)/i;
    
    url_new = url.replace(path_finder, "$1" + root_folder + "/");