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
 |  Show 1 more ment

4 Answers 4

Reset to default 3

You 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