diff --git a/index.d.ts b/index.d.ts index b2ab84e..e69a967 100644 --- a/index.d.ts +++ b/index.d.ts @@ -242,6 +242,13 @@ export interface Options { ``` */ readonly sortQueryParameters?: boolean; + + /** + prefer js-regexp-lookbehind{@link https://caniuse.com/js-regexp-lookbehind} + + @default true + */ + readonly preferJsRegexpLookbehind?: boolean; } /** diff --git a/index.js b/index.js index f8297f5..cd5c6d7 100644 --- a/index.js +++ b/index.js @@ -70,6 +70,8 @@ export default function normalizeUrl(urlString, options) { removeSingleSlash: true, removeDirectoryIndex: false, sortQueryParameters: true, + // ref: https://caniuse.com/js-regexp-lookbehind + preferJsRegexpLookbehind: true, ...options, }; @@ -121,7 +123,27 @@ export default function normalizeUrl(urlString, options) { // Remove duplicate slashes if not preceded by a protocol if (urlObject.pathname) { - urlObject.pathname = urlObject.pathname.replace(/(? { + if (/^(?!\/)/g.test(p1)) { + return `${p1}/`; + } + + return '/'; + }); + } } // Decode URI octets