-1

I'm new to Regex and was wondering how to split this string s4://test-dev/prefixes/file.mxf into ['test-dev', 'prefixes/file.mxf']. I want it to work for an unknown file path.

Ex) s4://test-dev/prefixes/file.mxf/newpath/anothernewpath into ['test-dev', 'prefixes/file.mxf/newpath/anothernewpath']

phaseharry
  • 369
  • 1
  • 3
  • 6

4 Answers4

0

Instead of splitting, use capture groups.

url.match(/^[^/]+:\/\/([^/]+)\/?(.*)/).slice(1)
TimTim Wong
  • 716
  • 4
  • 14
0

If you are using regex for splitting (which is not necessary) you can use this regex: s4:\/\/test-dev\/(.*) and use first group (first parentheses) as second string (first one is always same as i can see), but easiest way is to find position of third '/' with this var pos=str.split("/", i).join("/").length; and then find substring from that position to end: var res = str.substring(pos, str.length-pos);

stefan.mih
  • 98
  • 5
0

/^[\w+\:\/\/]+[-w+]/i matches 'text-dev'

/w+\:\/\/[-w+]\/gi/ matches the rest

  • The first regex matches your 'text-dev' or any text in that form coming after a protocol e.g text:// the second matches the rest of the text. Why not text it on the string first and see the output – Fritzdultimate Aug 03 '20 at 22:25
0

Please check the code below:

let url = "s4://test-dev/prefixes/file.mxf/newpath/anothernewpath"
let match = url.match(/^s4:\/\/([\w-]+)\/(.+)$/)
let result = [match[1], match[2]]
console.log(result)

The result is:

[
  "test-dev",
  "prefixes/file.mxf/newpath/anothernewpath"
]
marianc
  • 409
  • 1
  • 4
  • 13