Last active
October 26, 2018 09:58
-
-
Save blackmiaool/ca85fdde49f6463567871de6a40fdb50 to your computer and use it in GitHub Desktop.
addParam
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function addParam(url, key, value) { | |
url = url.replace(new RegExp(`[?&]${key}=[^&]*&?`, 'g'), str => (str[str.length - 1] === "&" ? str[0] : "")); | |
const hashReg = /#[\w-]+$/; | |
let hash = url.match(hashReg) || []; | |
hash = hash[0] || ""; | |
url = url.replace(hashReg, ""); | |
let linkSymbol = "&"; | |
if (url.indexOf("?") === -1) { | |
linkSymbol = "?"; | |
} | |
return `${url}${linkSymbol}${key}=${value}${hash}`; | |
} |
Author
blackmiaool
commented
Jul 9, 2018
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment