Nathan
Nathan

Reputation: 5372

Regular Expressions with part of a URI

I'm not very familiar with regular expressions.

I'm trying to create a regular expression that will match the text between the first group of two forwards slashes. It's easiest just to show an example.

Search Texts:
/index
/index/
/index/foo/
/index/foo/bar/
All of those should return just "index"

Another example:

Search Texts:
/page.php
/page.php/foo?bar=1
Should return just "page.php" for both of those

Thanks alot guys!

Upvotes: 0

Views: 105

Answers (1)

Nick Weaver
Nick Weaver

Reputation: 47241

Try this one for javascript or php preg_match: ^\/([^\/]*)

The pattern matches only of if there is a slash at the beginning and then matches everything that is not a slash.

Upvotes: 1

Related Questions