admin管理员组文章数量:1399887
Given string '/root/hello/hello/world'
I want to extract the second last ponent in a path i.e. the 2nd occurrence of hello.
If there's no parent part, I want it to return empty. So string /world
should return an empty string or null.
How can I extract the last path ponent using a regex or similar?
Language is javascript.
Given string '/root/hello/hello/world'
I want to extract the second last ponent in a path i.e. the 2nd occurrence of hello.
If there's no parent part, I want it to return empty. So string /world
should return an empty string or null.
How can I extract the last path ponent using a regex or similar?
Language is javascript.
Share Improve this question edited Nov 2, 2017 at 9:31 Ataur Rahman Munna 3,9151 gold badge25 silver badges35 bronze badges asked Nov 2, 2017 at 9:18 Richard GRichard G 5,70311 gold badges57 silver badges100 bronze badges 6- I didn't understand, what's your rule? Second to last or 2nd occurrence? Or does it have to respect both rules? – Rui Silva Commented Nov 2, 2017 at 9:21
- 1 second to last - so given a file name path, get the parent directory name. – Richard G Commented Nov 2, 2017 at 9:21
-
What about
"/world/"
? – Rajesh Commented Nov 2, 2017 at 9:22 - how about this: link It should help. – Rui Silva Commented Nov 2, 2017 at 9:27
- Why do you need to use regex? Are the solutions given (with split) enough? – Rui Silva Commented Nov 2, 2017 at 9:56
4 Answers
Reset to default 3You can firstly split
the string on the /
character to convert it into an array:
var split = '/root/hello/hello/world'.split('/')
-> ["", "root", "hello", "hello", "world"]
You can then grab the penultimate item:
var result = split[split.length - 2]
...but you may want to check the length of your array first:
var result;
if (split.length >= 2)
result = split[split.length - 2]
You can do
let str = '/root/hello/hello/world';
let result = str.split('/');
console.log(result[result.length-2]);
Using regex, as requested, you can do it with
([^/\n]*)\/[^/\n]*$
That captures the second to last part into capture group 1.
The ([^/\n]*)
part captures (inside parentheses) a stretch of characters that isn't a /
, nor a new line (\n
). The \/
ensures it's followed by a /
and [^/\n]*$
checks that the line finally is terminated by another stretch without a /
(or LF).
var pathArray = [
'/root/hello/cruel/world',
'/root/hello/world',
'/root/world',
'/world'
],
re = /([^/\n]*)\/[^/\n]*$/;
pathArray.forEach(function (path) {
document.write( '"' + path + '" returns "' + re.exec(path)[1] + '"<br/>' );
});
Try it out and experiment with it here at regex101.
You don't need regex, you can just split it.
const string = '/root/hello/hello/world';
// Split by the '/' character
const stringSplit = string.split('/');
// Slice the array, taking only the last 2 items.
// Then select the first one in the array
const myVal = stringSplit.slice(-2)[0];
// OR, using length
const myValLen = stringSplit[stringSplit.length - 2];
// OR, in one
const myValShort = string.split('/').slice(-2)[0];
本文标签: javascriptget second last component of a path from a string with js regexStack Overflow
版权声明:本文标题:javascript - get second last component of a path from a string with js regex - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744224990a2596046.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论