简体   繁体   English

如何检索从特定字符串开始到最后一个字符数组的字符

[英]How to retrieve characters starting from a specific string until last array of characters

row1: 10016/Documents/abc.pdf第 1 行: 10016/Documents/abc.pdf

row2: 10016-10017/10017/Documents/folder1/folder2/xyz.pdf第 2 行: 10016-10017/10017/Documents/folder1/folder2/xyz.pdf

I'm trying to retrieve all the characters starting from /Documents but without the last part (file name)我正在尝试检索从 /Documents 开始但没有最后一部分(文件名)的所有字符

In row 1, I want to retrieve /Documents/在第 1 行,我想检索/Documents/

In row 2, I want to retrieve /Documents/folder1/folder2/在第 2 行,我想检索/Documents/folder1/folder2/

I tried我试过

var temp1 = FullPath.split("/Documents/")[0];

var A_Fpath = temp1.split("/");

A_Fpath = A_Fpath[A_Fpath.length - 1];

A simple regex would do the trick:一个简单的正则表达式可以解决问题:

/\/Documents.*\//
/                    start the regex
 \/                  match literally a "/" (the \ is to escape the / reserved character)
   Documents         match literally the word "Documents" (case sensitive
            .*       match 0 or more characters (any characters)
              \/     match literally a "/"
                /    end the regex
   
This works because regex will attempt to match the longest possible string
of characters that match the regex.

 const row1 = "10016/Documents/abc.pdf"; const row2 = "10016-10017/10017/Documents/folder1/folder2/xyz.pdf"; const regex = /\\/Documents.*\\//; const val1 = row1.match(regex)[0]; const val2 = row2.match(regex)[0]; console.log(val1); console.log(val2);

Here's a Regex101 link to test it out and see more info about this specific regex.这是一个Regex101链接,可以对其进行测试并查看有关此特定正则表达式的更多信息。

If javascript had a grown-up regular expression engine, one could use a positive, non-capturing lookahead group to determine when to stop.如果 javascript 有一个成熟的正则表达式引擎,则可以使用一个积极的、非捕获的前瞻组来确定何时停止。

Since javascript lacks that, the simple, clearer, and more efficient way is to not use a regular expression at all.由于 javascript 缺少这一点,因此更简单、更清晰、更有效的方法是根本不使用正则表达式。 The algorithm is simple:算法很简单:

  • Find the [first/leftmost] /Documents in the source text, then找到源文本中的[first/leftmost] /Documents ,然后

  • Find the last/rightmost occurrence of / in the source text查找源文本中最后/最右边出现的/

  • Deal with the two special cases where:处理以下两种特殊情况:

    • The source string doesn't contain /Documents at all, and源字符串根本不包含/Documents ,并且
    • The rightmost / is the / in /Documents最右边的///Documents
  • Failing a special case as noted above, return the desired substring extending from /Documents up to and including the last /如果上述特殊情况失败,则返回从/Documents扩展到并包括最后一个/的所需子字符串

Like this:像这样:

function getInterestingBitsFrom(path) {
  const i   = path.indexOf('/Documents');
  const j   = path.lastIndexOf('/');
  const val = i < 0   ? undefined     // no '/Documents' in string
            : i === j ? path.slice(i) // last '/' in string is the '/' in '/Documents'             
            : path.slice(i, j+1)      // '/Documents/' or '/Documents/.../'
            ;

  return retVal;  
}

This also has the laudatory benefit of being easy to understand for someone who has to figure out what you were trying to accomplish.对于那些必须弄清楚您想要完成什么的人来说,这也具有易于理解的优点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM