如何在第一个斜杠之后的 url 中提取第二个值,在一个可能有很多斜杠的字符串中?
How to pluck the second value in a url after the first slash, in a string that could have many slashes?
我知道那里有无数的正则表达式问题,但我找不到适合我情况的问题。
假设我有以下 pathname
:
/u/some_user/create/initial
如何从此字符串中提取 'some_user'
?
我已经非常接近这个了:
const pathname = '/u/some_user/create/initial';
const result = pathname.match(/(\/u\/)(.{1,}\/)(.+)/);
console.log('result', result);
如果字符串是 '/u/some_user/create'
,这个 可能 可能起作用——它会 return some_user/
,我可以过滤掉斜线在末尾。但是如果字符串有更多的斜杠,如上,那么这只是 returns 'some_user/create/'
.
怎样才能实现只拔'some_user'
?
如果您不关心旧版浏览器支持,包括 Safari 和安装在 Mac/iOS 上的任何浏览器,那么这可行:
/(?<=^\/u\/)[^\/]+/
var regexp = /(?<=^\/u\/)[^\/]+/;
console.log( `/u/some_user/create/initial`.match( regexp ) );
console.log( `/u/`.match( regexp ) );
console.log( `/wrong/format/url`.match( regexp ) );
console.log( `/u/another_user/create/initial`.match( regexp ) );
您可以使用捕获组和 negated character class:
\/u\/([^/]+)
说明
\/u\/
匹配 /u/
(
捕获 组 1
[^/]+
使用否定字符 class. 匹配 /
以外的 1+ 个字符
)
关闭组 1
看到一个regex101 demo。
const regex = /\/u\/([^/]+)/;
[
"/u/some_user/create/initial",
"/u/some_user/create",
"test/123/u/some_user/a/b/c"
].forEach(s => {
const m = s.match(regex);
if (m) {
console.log(m[1]);
}
});
如果不想跨行,some_user后面要有/
:
\/u\/([^/\s]+)\/
再看一个regex 101 demo。
尽管要求正则表达式,但使用两种常见的字符串方法中的任何一种都可以轻松解决特定问题。
string.split
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split
Returns 根据作为参数提供的任何一个或多个字符划分的子字符串数组。通过在每个斜杠处拆分字符串,所需的字符串是结果数组的元素 [2](因为元素 [0] 将为空 - 从第一个“/”之前的 0 个字符派生而来)。
即:
const url = "/u/some_user/create/initial";
requiredString = url.split("/")[2];
console.log(requiredString); // "some_user";
字符串切片
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/slice
如果目标子串的位置(无论是数字的还是参考已知的边界测试)可用,string.slice
提供了一种提取所需子串的简单方法。
在此示例中,边界文本部分用于提取所需字段:
const url = "/u/some_user/create/initial";
beforeText = "/u/";
afterText = "/create";
let requiredString = url.slice(url.indexOf(beforeText)+beforeText.length, url.indexOf(afterText));
console.log(requiredString); // "some-user"
这两种方法都是可靠的核心javascript,所有解释器都支持。
我知道那里有无数的正则表达式问题,但我找不到适合我情况的问题。
假设我有以下 pathname
:
/u/some_user/create/initial
如何从此字符串中提取 'some_user'
?
我已经非常接近这个了:
const pathname = '/u/some_user/create/initial';
const result = pathname.match(/(\/u\/)(.{1,}\/)(.+)/);
console.log('result', result);
如果字符串是 '/u/some_user/create'
,这个 可能 可能起作用——它会 return some_user/
,我可以过滤掉斜线在末尾。但是如果字符串有更多的斜杠,如上,那么这只是 returns 'some_user/create/'
.
怎样才能实现只拔'some_user'
?
如果您不关心旧版浏览器支持,包括 Safari 和安装在 Mac/iOS 上的任何浏览器,那么这可行:
/(?<=^\/u\/)[^\/]+/
var regexp = /(?<=^\/u\/)[^\/]+/;
console.log( `/u/some_user/create/initial`.match( regexp ) );
console.log( `/u/`.match( regexp ) );
console.log( `/wrong/format/url`.match( regexp ) );
console.log( `/u/another_user/create/initial`.match( regexp ) );
您可以使用捕获组和 negated character class:
\/u\/([^/]+)
说明
\/u\/
匹配/u/
(
捕获 组 1[^/]+
使用否定字符 class. 匹配
/
以外的 1+ 个字符)
关闭组 1
看到一个regex101 demo。
const regex = /\/u\/([^/]+)/;
[
"/u/some_user/create/initial",
"/u/some_user/create",
"test/123/u/some_user/a/b/c"
].forEach(s => {
const m = s.match(regex);
if (m) {
console.log(m[1]);
}
});
如果不想跨行,some_user后面要有/
:
\/u\/([^/\s]+)\/
再看一个regex 101 demo。
尽管要求正则表达式,但使用两种常见的字符串方法中的任何一种都可以轻松解决特定问题。
string.split
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split
Returns 根据作为参数提供的任何一个或多个字符划分的子字符串数组。通过在每个斜杠处拆分字符串,所需的字符串是结果数组的元素 [2](因为元素 [0] 将为空 - 从第一个“/”之前的 0 个字符派生而来)。
即:
const url = "/u/some_user/create/initial";
requiredString = url.split("/")[2];
console.log(requiredString); // "some_user";
字符串切片
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/slice
如果目标子串的位置(无论是数字的还是参考已知的边界测试)可用,string.slice
提供了一种提取所需子串的简单方法。
在此示例中,边界文本部分用于提取所需字段:
const url = "/u/some_user/create/initial";
beforeText = "/u/";
afterText = "/create";
let requiredString = url.slice(url.indexOf(beforeText)+beforeText.length, url.indexOf(afterText));
console.log(requiredString); // "some-user"
这两种方法都是可靠的核心javascript,所有解释器都支持。