2

I want to match everything before the nth character (except the first character) and everything after it. So for the following string

/firstname/lastname/some/cool/name

I want to match

Group 1: firstname/lastname
Group 2: some/cool/name

With the following regex, I'm nearly there, but I can't find the correct regex to also correctly match the first group and ignore the first /:

([^\/]*\/){3}([^.]*)

Note that I always want to match the 3rd forward slash. Everything after that can be any character that is valid in an URL.

Stephan
  • 1,791
  • 1
  • 27
  • 51

3 Answers3

1

Your regex group are not giving proper result because ([^\/]*\/){3} you're repeating captured group which will overwrite the previous matched group Read this

You can use

^.([^/]+\/[^/]+)\/(.*)$

enter image description here

let str = `/firstname/lastname/some/cool/name`

let op = str.match(/^.([^/]+\/[^/]+)\/(.*)$/)

console.log(op)
Code Maniac
  • 37,143
  • 5
  • 39
  • 60
  • Works. But I don't get why. How did you create this neat little graphic? – Stephan Jul 14 '19 at 13:41
  • @Stephan where you're facing problem to understand ? this little graphics is generated by [`Regex visualizer`](https://jex.im/regulex/) – Code Maniac Jul 14 '19 at 13:52
  • The more I look at it the more clear it is. The only thing missing now is the first two characters: ^. I guess they are used to ignore the first character. But how? – Stephan Jul 14 '19 at 14:04
  • @Stephan `^` means start of string and `.` means match anything except new line, since we don't want this to be included in our group so we kept it out of `()` – Code Maniac Jul 14 '19 at 14:06
0

Ignoring the first /, then capturing the first two words, then capturing the rest of the phrase after the /.

^(:?\/)([^\/]+\/[^\/]+)\/(.+)

See example

Yaelet
  • 160
  • 10
0

The quantifier {3} repeats 3 times the capturing group, which will have the value of the last iteration.

The first iteration will match /, the second firstname/ and the third (the last iteration) lastname/ which will be the value of the group.

The second group captures matching [^.]* which will match 0+ not a literal dot which does not take the the structure of the data into account.

If you want to match the full pattern, you could use:

^\/([^\/]+\/[^\/]+)\/([^\/]+(?:\/[^\/]+)+)$

Explanation

  • ^ Start of string
  • ( Capture group 1
  • ) Close group
  • \/ Match /
  • ( Capture group 2
    • [^\/]+ Match 1+ times not /
    • (?:\/[^\/]+)+ Repeat 1+ times matching / and 1+ times not / to match the pattern of the rest of the string.
  • ) Close group
  • $ End of string

Regex demo

The fourth bird
  • 154,723
  • 16
  • 55
  • 70