DAILY DOCDAILY DOC
Rust
Node
Notes
Ubuntu
Leetcode
  • it-tools
  • excalidraw
  • linux-command
Rust
Node
Notes
Ubuntu
Leetcode
  • it-tools
  • excalidraw
  • linux-command
  • BFC 块级格式化上下文
  • Note
  • WebAssembly
  • public api
  • 位运算
  • bitwise operator
  • css实现隐藏效果
  • css snippets
  • 抖音点赞
  • js 相等判断
  • fetch ReadableStream
  • git
  • Github Actions 工作流
  • google search
  • RPC vs HTTP
  • gravatar
  • hhkb
  • Init project
  • input 文件上传
  • mac

    • Mac 使用技巧
    • alfred
    • mac shortcuts
    • shortcuts text edit
    • mac 修改host
  • 微前端
  • mock
  • nginx dump
  • nginx
  • NirCmd
  • npm
  • Operator Precedence
  • package.json
  • url query 解析
  • pnpm
  • JavaScript Precise countdown
  • react 模版
  • regexp
  • setup web development
  • telegram

    • telegram bot
  • timeFunction ease
  • 视频裁剪
  • vscode

    • vscode 高级指南
    • bracketPairs
    • jsconfig.json
    • vscode pipe into code
    • social project
    • vscode tasks
  • draggable resizable
  • windows 激活
  • 前端截图实现
  • 文本配音 富文本实现
  • 图片处理
  • 前端坐标
  • 定时任务
  • work efficient
  • 微信小程序动画实现方案
  • 排列组合
  • 数列
  • 语音驱动文字
  • 浏览器
  • 状态管理
  • 移动盒子
  • 移动端开发常用snippets
  • 设计模式
  • web performance

url query 解析

  1. url参数解析为对象
  2. url参数解析为数组
  3. URLSearchParams(兼容性有待提高,慎用)

url参数解析为对象

const urlQueryToObject = (url) => {
    if((/\?/).test(url)) {
        const arr =  url.split('?')[1].split('&');

        return  arr.reduce((acc,value,index) => {value.replace(/(\w+)=(\w+)/,(match,p1,p2) => {acc[p1] = p2});return acc},{});
    }
    return {}
}

const getURLParameters = url =>
  (url.match(/([^?=&]+)(=([^&]*))/g) || []).reduce(
    (a, v) => ((a[v.slice(0, v.indexOf('='))] = v.slice(v.indexOf('=') + 1)), a),
    {}
);
// example
// location.href ='http://example.com/user?id=1&age=2'
const paramsObj = urlQueryToObject(location.href)  //{id: "1", age: "2"}
paramsObj.id 
paramsObj.age

url参数解析为数组

const urlQueryToArr = url => {
    if((/\?/).test(url)) {
        const arr = url.split('?')[1].split('&');
        return arr.reduce((acc,value,index) => {acc.push(value.split('='));return acc;},[])
    }
    return []
}
// example
// location.href ='http://example.com/user?id=1&age=2' 
JSON.stringify(urlQueryToArr(location.href)) //  "[["id","1"],["age","2"]]"

URLSearchParams

/**
 * 
 * @param {string} url 
 * @return URLSearchParams ;
 */
const urlParams = url => {
    if(/\?/.test(url)) {
        return searchParams = new URLSearchParams( url.split('?')[1]);
    }
    return new URLSearchParams()
}

usage

// location.href ='http://example.com/user?id=1&age=2' 
const params = urlParams(location.href);  //iterater obj
params.get('id') // 1
params.has('id') // true
params.append('id','2')
params.getAll('id')  //   ["1", "2"]
params.set('id','0') 
params.get('id') //  "0"
params.getAll('id') //  ["0"]
params.delete('id')
params.get('id')  // null
params.toString() //"age=2"

Refer:

  • MDN:URLSearchParams
Last Updated:
Contributors: rosendo
Prev
package.json
Next
pnpm