gpt4 book ai didi

regex - URL 上的正则表达式

转载 作者:行者123 更新时间:2023-12-02 08:41:01 27 4
gpt4 key购买 nike

我的正则表达式很差,需要一些小助手。我正在尝试从 URL 中获取 41398。这个数字是一个变量

http://www.website.com/beta/flips/index/41398_1363874140/friend/756

我想出了如何获取索引/之后的所有内容。

var url      = document.URL;
var myRegexp = /index(.*)/;
var match = myRegexp.exec(url);

这给我留下了/41398_1363874140/friend/756....

是否有正则表达式的解决方案来获取/和 _ 之间的数字?

我试过了

var url      = document.URL;
var myRegexp = \/(.*?)\_;
var match = myRegexp.exec(url);

当然 '/' 是问题所在。

最佳答案

如果您知道总会有一个下划线,您可以使用:

var url      = document.URL;
var myRegexp = /\d+_/;
var match = myRegexp.exec(url);

关于regex - URL 上的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16447674/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com