Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

写一个提取 URL 参数的 JS 方法 #3

Open
heycn opened this issue Mar 8, 2023 · 0 comments
Open

写一个提取 URL 参数的 JS 方法 #3

heycn opened this issue Mar 8, 2023 · 0 comments

Comments

@heycn
Copy link
Owner

heycn commented Mar 8, 2023

写一个提取 URL 参数的 JS 方法

例如:url="https://alibaba.com?a=1&b=2&c=3#/abc/def?a=2&b=3&c=4"

要求:只识别queryParam,排除 hash 的干扰

const url = 'https://alibaba.com?a=1&b=2&c=3#/abc/def?a=2&b=3&c=4'
const getParamFromURL = (url, key) => {
  const str = new RegExp(`/\?${key}=(.+)\#/`)
  return (str.exec(url))[1][0]
}

getParamFromURL(url, 'c') // '3'
@heycn heycn closed this as completed Mar 8, 2023
@heycn heycn reopened this Mar 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant