javascript - 正则表达式:匹配,但不包括匹配的部分

标签 javascript regex

这是正文:

https://www.google.com/url?rct=3Dj\u0026sa=3Dt\u0026url=3Dhttps://rivesjournal.com/inside-track-trading-focus-on-shares-of-adobe-systems-inc-adbe/48453/\u0026ct=3Dga\u0026cd=3DCAEYASoTOT

我想获取实际链接:

https://rivesjournal.com/inside-track-trading-focus-on-shares-of-adobe-systems-inc-adbe/48453/

/=3Dhttps.*\//g 包含 =3D,但我想去掉它。我该如何解决这个问题?

这是 regex .

最佳答案

一个选项是通过使用带有 ^ anchor 的否定前瞻来防止第一个 http.* 子串被匹配:

Example Here

(?!^)https:.*\/

这基本上匹配 https:.*\/ 只要它不在字符串的开头。

片段:

var string = 'https://www.google.com/url?rct=3Dj\u0026sa=3Dt\u0026url=3Dhttps://rivesjournal.com/inside-track-trading-focus-on-shares-of-adobe-systems-inc-adbe/48453/\u0026ct=3Dga\u0026cd=3DCAEYASoTOT';

console.log(string.match(/(?!^)https:.*\//)[0]);


但是,上面的表达式不会涵盖所有边缘情况,因此更好的选择是只使用捕获组:

Updated Example

=3D(https.*\/)

片段:

var string = 'https://www.google.com/url?rct=3Dj\u0026sa=3Dt\u0026url=3Dhttps://rivesjournal.com/inside-track-trading-focus-on-shares-of-adobe-systems-inc-adbe/48453/\u0026ct=3Dga\u0026cd=3DCAEYASoTOT';

console.log(string.match(/=3D(https.*\/)/)[1]);


您还可以使用否定字符类,例如 [^\\]+ 以匹配一个或多个非 \ 字符:

Updated Example

=3D(https[^\\]+)

关于javascript - 正则表达式:匹配,但不包括匹配的部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42353775/

相关文章:

javascript - jQuery .each() 获取多个div的值

javascript - 在 for 循环中使用 x.length 时出现问题

regex - Postgres asterisc 正则表达式量词不起作用

regex - R:使用 grep/gsub 查找重复模式

Java RegEx 和换行符 - 错误或预期行为?

javascript - 为什么javascript setTimeout 延迟不起作用并且 setInterval 太慢

javascript - TypeScript 类型 'string | null' 无法分配给类型 'string'

javascript - 函数计算错误

javascript - 用于删除数字的正则表达式

regex - 在两个字符串之间替换文本