diff --git a/lib/url.js b/lib/url.js index 016acd08ca774d..d3fcdc3c1366ef 100644 --- a/lib/url.js +++ b/lib/url.js @@ -413,7 +413,7 @@ Url.prototype.format = function() { pathname = pathname.replace(/[?#]/g, function(match) { return encodeURIComponent(match); }); - search = search.replace('#', '%23'); + search = search.replace(/#/g, '%23'); return protocol + host + pathname + search + hash; }; diff --git a/test/parallel/test-url.js b/test/parallel/test-url.js index 08f13c809df51e..b395c8909696b0 100644 --- a/test/parallel/test-url.js +++ b/test/parallel/test-url.js @@ -1127,6 +1127,19 @@ var formatTests = { hash: '#frag', search: '?abc=the#1?&foo=bar', pathname: '/fooA100%mBr', + }, + + // multiple `#` in search + 'http://example.com/?foo=bar%231%232%233&abc=%234%23%235#frag': { + href: 'http://example.com/?foo=bar%231%232%233&abc=%234%23%235#frag', + protocol: 'http:', + slashes: true, + host: 'example.com', + hostname: 'example.com', + hash: '#frag', + search: '?foo=bar#1#2#3&abc=#4##5', + query: {}, + pathname: '/' } }; for (const u in formatTests) {