0

I'm trying to get a regex for selecting part of a network path

\\server.env.com\Target\Test1\Test2\final1\final2\final3\final4\final5

I need to skip two folders after Target and get the rest of the path from the above. So regex should give me final1\final2\final3\final4\final5 in this case. The path can have more levels of folders after final5. So the regex should work for any number of folders.

When I am using look behind, the browser says its not supported, so cannot use it.

treeseal7
  • 627
  • 7
  • 18
gcride
  • 23
  • 3
  • 2
    Welcome to Stackoverflow! Please read how to ask [**a minimal, complete and verifiable**](https://stackoverflow.com/help/mcve) question, edit your question and give us more information (ie programming language, etc.). – Jan Sep 25 '17 at 20:10
  • do you know what the target will be called? what makes it obvious that it is 'target'? – treeseal7 Sep 25 '17 at 20:10
  • also javascript doesn't support look behind only look forwards. There are workarounds though, for example reversing the string first but this is likely not needed – treeseal7 Sep 25 '17 at 20:14
  • I would rather split the string at '\' and then accomplish your result with a loop and counting... – futu Sep 25 '17 at 20:45

2 Answers2

0

Using regex...

var str1="path \\server.env.com\\Target\\Test1\\Test2\\final1\\final2\\final3\\final4\\final5"
//  "path \server.env.com\Target\Test1\Test2\final1\final2\final3\final4\final5"

str1.match( /([^\\]*\\){5}(.*)/ )[2]
//  "final1\final2\final3\final4\final5"

works based on a test for number of forward slashes prior to the 'finals'

Or, using split

var arr = str1.split("\\")
arr.splice(0,5)
var result = arr.join("\\")

result // "final1\final2\final3\final4\final5"
treeseal7
  • 627
  • 7
  • 18
  • Both of these solutions rely on Target to be at a specific place in the string – Will Barnwell Sep 25 '17 at 23:33
  • i know... @gcride didn't say how target would be defined and whether we know the name of target. can't but try eh – treeseal7 Sep 25 '17 at 23:36
  • try making reasonable assumptions about what OP is trying to do and keep in mind the point of a good answer on SO is to solve the problem in a general way that will help people who find this question in the future – Will Barnwell Sep 25 '17 at 23:38
  • that's absolutely what I've done is it not? (note the comment after the question trying to clarify the 'target' issue) – treeseal7 Sep 25 '17 at 23:57
  • no, what's petty is you downvoting my answer as 'revenge', I downvoted this answer because I consider it to be brittle and not generalizable – Will Barnwell Sep 26 '17 at 00:21
  • There's nothing petty about using the voting system that stack overflow is based on to support or suppress answers that i think are good or bad – Will Barnwell Sep 26 '17 at 00:26
  • no, I was just treating your answer with the same brush you treated mine with, seems fair right. people are driven away from SO by people like you. I put effort into answering someone else's question and offered 2 solutions that do work and all I got was negativity from some bloke called will. great. – treeseal7 Sep 26 '17 at 00:29
  • 1
    Criticism != negativity I'm sorry that you took personal offense to your answer being downvoted. OP and other community members will eventually weigh in with their own votes. If SO didn't have downvoting the quality of the site would significantly decrease and would stop being as useful a resource. Commenting why I don't think your answer is a good one and downvoting is how things happen. I sometimes write really dumb answers that get the heck downvoted out of them. You are totally within your right to downvote my answer, but I am concerned that it may be emotion driven. – Will Barnwell Sep 26 '17 at 00:39
  • Also if you care alot about internet points, I found this question because I read it to approve your edit, which I voted to approve. As long as somebody agrees with me in the review queue you'll be net 0 on rep change – Will Barnwell Sep 26 '17 at 00:43
  • yeah fine, if your comment makes sense then i'll accept it but if it's condescending and petty then expect something back. – treeseal7 Sep 26 '17 at 00:51
  • No worries, glad it helped! – treeseal7 Sep 26 '17 at 14:31
  • @gcride you should upvote the answer if you found it well written and helpful please – Will Barnwell Sep 26 '17 at 17:21
  • can you do that with 3 points? think you can only accept answers? – treeseal7 Sep 26 '17 at 17:23
0

Following this post: How do you use a variable in a regular expression?

Create a regex that replaces everything up to Target and then two more sub-directories

var path = "\\server.env.com\\Target\\Test1\\Test2\\final1\\final2\\final3\\final4\\final5"
console.log("Path is: " + path)
var target = "Target"
var regex = "^.*" + target + "\\\\(?:[^\\\\]+\\\\){2}"
console.log("Regex is: "+ regex)
var re = new RegExp(regex, "mg")
var extracted = path.replace(re, "")
console.log("Extraction is: " + extracted)
Will Barnwell
  • 3,729
  • 18
  • 33
  • this assumes that target is of a known name – treeseal7 Sep 26 '17 at 00:01
  • I assumed based on the way OP asked that Target is a known string that will be known by the time this trimming is needed, if the question was about how to capture a certain number of repeating patterns OP would have mentioned that number and I would have flagged it as a dupe, not answered. – Will Barnwell Sep 26 '17 at 00:24