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

regexp

Assetions

  • ^ Start
  • $ End
  • \b Word boundary
  • \B None word boundary
  • x(?=y) Match x only x followed by y
  • x(?!y) Match x only y not followed by y
  • (?<=y)x Math x if x follows y
  • (?<!y)x Math x if x not follows y

Refer

  • Assertions mdn

Charactor Classes

  • \ Escape
  • \cX
  • \xhh
  • \uhhhh
  • \u{hhhh} or \u{hhhhh}
  • \

Group and Rangers

(x) 捕获括号

捕获组: 匹配x并记住匹配项。例如,/(foo)/匹配并记住“foo bar”中的“foo” 。捕获组会带来性能损失。如果不需要收回匹配的子字符串,请选择非捕获括号(?:x)

string.match()

  • 如果使用g标志,则将返回与完整正则表达式匹配的所有结果,但不会返回捕获组。
  • 如果未使用g标志,则仅返回第一个完整匹配及其相关的捕获组(Array)可使用 matchAll

string.machAll() RegExp必须是设置了全局模式g的形式

regxp.exec()

const regexp = RegExp('foo[a-z]*','g');
const str = 'table football, foosball';
let match;

while ((match = regexp.exec(str)) !== null) {
  console.log(`Found ${match[0]} start=${match.index} end=${regexp.lastIndex}.`);
  // expected output: "Found football start=6 end=14."
  // expected output: "Found foosball start=16 end=24."
}
`foo bar`.match(/(foo)/) //  返回完整匹配 以及捕获组
// ['foo', 'foo', index: 0, input: 'foo bar', groups: undefined]

`foo bar`.match(/(foo)/g) // 设置了 `g` flag 不会返回捕获组
// ['foo']

`foo bar`.match(/foo/)
// ['foo', index: 0, input: 'foo bar', groups: undefined]

'test1test2'.matchAll( /t(e)(st(\d?))/g) // 必须携带 `g` flag 
// ['test1', 'e', 'st1', '1', index: 0, input: 'test1test2', groups: undefined]
// ['test2', 'e', 'st2', '2', index: 5, input: 'test1test2', groups: undefined]

string.replace()

`aabbcdef`.replace(/\w/,'$$')
// '$abbcdef'
`aabbcdef`.replace(/\w/,'$&')
// 'aabbcdef'
`aabbcdef`.replace(/(\w)/,'$&')
// 'aabbcdef'
`a1abbcdef`.replace(/(\d)/,'$&')
// 'a1abbcdef'
`a1abbcdef`.replace(/(\d)/,'$&')
// 'a1abbcdef'
`a1abbcdef`.replace(/\d/,'$$')
// 'a$abbcdef'
`a1abbcdef`.replace(/\d/,'$&')
// 'a1abbcdef'
`a1abbcdef`.replace(/\d/,'$`')
// 'aaabbcdef'
`aX1abbcdef`.replace(/\d/,'$`')
// 'aXaXabbcdef'
`aX1abbcdef`.replace(/\d/,"$'")
// 'aXabbcdefabbcdef'
`aX1abbcdef`.replace(/\d/,'$1')
// 'aX$1abbcdef'
`aX1abbcdef`.replace(/(\d)/,'$1')
// 'aX1abbcdef'


`aX1abbcdef`.replace(/(\d)/,(...args) => {
    console.log(args)
    //  ['1', '1', 2, 'aX1abbcdef']
    // match p1 offset string
})


abc|def abc或者 def

[abc] [^abc] 字符集

\n 反向引用捕获括号

其中n是一个正整数。对正则表达式中与n括号匹配的最后一个子字符串的反向引用(计算左括号)。例如,/apple(,)\sorange\1/ 匹配 “apple,orange,cherry,peach” 中的 "apple,orange,", 其中 \1 引用了 之前使用 () 捕获的 ,

`apple,orange,cherry,peach`.match(/apple(,)orange\1/) // `\1`表示第一个捕获括号
//  ['apple,orange,', ',', index: 0, input: 'apple,orange,cherry,peach', groups: undefined]

(?:x) 非捕获括号

`apple,orange,cherry,peach`.match(/apple(?:,)orange\1/)
// null
`apple,orange,cherry,peach`.match(/apple(?:,)orange/)
// ['apple,orange', index: 0, input: 'apple,orange,cherry,peach', groups: undefined]

非捕获组: 匹配 “x”,但不记得匹配。

Quantifiers 量词

默认情况下 量词用的是贪婪模式 ,量词后接 ? 表示非贪婪模式

  • x*?
  • x+?
  • x??
  • x{n}?
  • x{n,}?
  • x{n,m}?
`some <foo> <bar> new </bar> </foo> thing`.match(/<.*>/)
// ['<foo> <bar> new </bar> </foo>', index: 5, input: 'some <foo> <bar> new </bar> </foo> thing', groups: undefined]

`some <foo> <bar> new </bar> </foo> thing`.match(/<.*?>/) // 非贪婪模式
// ['<foo>', index: 5, input: 'some <foo> <bar> new </bar> </foo> thing', groups: undefined]

query解析

/([^?=&]+)(=([^&]*)

const getURLParameters = url =>
  (url.match(/([^?=&]+)(=([^&]*))/g) || []).reduce(
    (a, v) => ((a[v.slice(0, v.indexOf('='))] = v.slice(v.indexOf('=') + 1)), a),
    {}
  );

query对象转成queryString

Object.entries(aa).reduce((acc,item) =>(acc+=`&${item[0]}=${item[1]}`,acc),'').slice(1)

Refer

  • mdn regexp
Last Updated:
Contributors: rosendo
Prev
react 模版
Next
setup web development