-1

I am using a regular expression to separate elements of a url:

http://domain.com/page/post#fragment?query

/(?:([\w\-\+]+(?<!domain\.com))\/?)?(?:([\w\-\+]+(?<!domain\.com))\/?)?(?:#([\w\-\+]+))?(?:\?([\w\-\+]+))?$/

Here would be the results of this match:

$1 = page

$2 = post

$3 = fragment

$4 = query

I am getting this error:

Invalid regular expression: /(?:([\w\-\+]+(?<!localhost))\/?)?(?:([\w\-\+]+(?<!localhost))\/?)?(?:#([\w\-\+]+))?(?:\?([\w\-\+]+))?$/: Invalid group
godismyjudge95
  • 922
  • 1
  • 9
  • 15
  • 1
    JavaScript regex supports lookbehind now. See [Javascript: negative lookbehind equivalent](https://stackoverflow.com/questions/641407/javascript-negative-lookbehind-equivalent) – Ryszard Czech Oct 20 '20 at 19:31

2 Answers2

7

This part: (?<!domain\.com)) is the problem since Javascript doesn't support lookbehind.

anubhava
  • 761,203
  • 64
  • 569
  • 643
  • 2
    Correct (+1). To emulate it in JS, read: http://blog.stevenlevithan.com/archives/mimic-lookbehind-javascript – BlackVegetable Nov 18 '13 at 18:20
  • So could you give me an example of how I might use the method in the article to simulate the same effect? – godismyjudge95 Nov 18 '13 at 18:57
  • @LeinardoSmtih: You can use `window.location.pathname` to get URI part after `http://domain.com` and then you can rest of your regex. Also see this article: http://css-tricks.com/snippets/javascript/get-url-and-url-parts-in-javascript/ – anubhava Nov 18 '13 at 19:03
0

You might be able to tweak this to suit your needs, it captures most of the different parts of the URL:

^(?:([A-Za-z]+):)?(\/{0,3})([0-9.\-A-Za-z]+)(?::(\d+))?(?:\/([^?#]*))?(?:\?([^#]*))?(?:#(.*))?$

Fiddle: http://www.rexfiddle.net/m1ERMlZ

Note: This doesn't work with multiple URLs separated by line breaks, so using the g or m flags doesn't do anything.


If you're just trying to tokeninze a URL (i.e. grab the different parts out of it individually), try this simple method: https://gist.github.com/jlong/2428561

qJake
  • 16,821
  • 17
  • 83
  • 135
  • That captures all of the folders in one variable. I wanted the regex to do the work of separating the folders into different variables. – godismyjudge95 Nov 18 '13 at 18:56
  • I'm not sure if you can have dynamically-generated capture groups with Javascript regex (e.g. one capture group per folder). At a certain point of complexity, Regex stops being useful and starts being a burden because you're trying to force Regex to do more than it is capable of. For example, the .NET framework has its own dedicated class for parsing, validating, and handling URIs/URLs. If you're just looking to extract the parts of a URI, try this method: https://gist.github.com/jlong/2428561. Edited my answer to include this. – qJake Nov 18 '13 at 21:03
  • It is a little more complicated than that... I have a false folder structure setup with the htaccess file and that is how the htaccess file interprets the url. I am trying to stay consistant across all of the various file types dissecting the url. – godismyjudge95 Nov 18 '13 at 23:17