1

I'm not very good with regex

I want a regex expression that matches these:

http://localhost:3000/categories/football
http://localhost:3000/categories/football/2222/45444
http://localhost:3000/categories/tennis/45454
http://localhost:3000/categories/football/12
http://localhost:3000/categories/cricket/

basically, for all the above potential URL paths, I want the words after categories/ and before the second / to be returned

i.e.

football, football, tennis, football, cricket

I got this far: (categories)/\w+

but obviously, that still includes categories

6
  • just use a group for the \w+ part. You will get the content with $2 Commented May 25, 2018 at 11:40
  • 1
    var categories = location.pathname.split("/")[2]; Commented May 25, 2018 at 11:43
  • I would just go for splitting on the slash instead. const [ protocol, host, page, topic, id, reference ] = route.replace( '://', '/' ).split( '/' ); return topic; Commented May 25, 2018 at 11:44
  • 1
    @AndrewBone I was considering that, but then a regex is more readable Commented May 25, 2018 at 11:46
  • 1
    @mplungjan things you never expect to read, "regex is more readable" 😉 Commented May 25, 2018 at 11:48

5 Answers 5

2

You could change the capturing group from (categories) to (\w+) and use a positive lookahead (?= to assert that what followes is an optional (?=\/?) forward slash.

The values you are looking for are in captured group 1.

categories\/(\w+)(?=\/?)

const strings = [
  "http://localhost:3000/categories/football",
  "http://localhost:3000/categories/football/2222/45444",
  "http://localhost:3000/categories/tennis/45454",
  "http://localhost:3000/categories/football/12",
  "http://localhost:3000/categories/cricket/"
];
let pattern = /categories\/(\w+)(?=\/?)/;

strings.forEach((s) => {
  console.log(s.match(pattern)[1]);
});

Sign up to request clarification or add additional context in comments.

Comments

2

Why not just split - if the category is the second in the pathname always

var loc = "/categories/football/2222/45444"; // var loc = location.pathname
console.log(loc.split("/")[2])

1 Comment

I would prefer this answer
1

Here you go.

var url1 = "http://localhost:3000/categories/football"
var url2 = "http://localhost:3000/categories/football/2222/45444"
var url3 = "http://localhost:3000/categories/tennis/45454"
var url4 = "http://localhost:3000/categories/football/12"
var url5 = "http://localhost:3000/categories/cricket/"

var regex = /categories\/(\w+)(?=\/?)/;
console.log(regex.exec(url1)[1]);
console.log(regex.exec(url2)[1]);
console.log(regex.exec(url3)[1]);
console.log(regex.exec(url4)[1]);
console.log(regex.exec(url5)[1]);

2 Comments

That's the same as an answer from a few minutes ago 🙂
while i was typing the answer someone has posted the same :-D
0

You can use the RegEx (?<=categories\/)[^\/\n]+

  • (?<=categories\/) makes sure your match is preceded by categories/

  • [^\/\n]+ matches anything but a / or a newline 1 or more times

Demo at Regex101.

var re =  /(?<=categories\/)[^\/\n]+/
console.log(
"http://localhost:3000/categories/football/2222/45444".match(re)[0]
)

1 Comment

This will only work in Chrome until EchmaScript 2018 is implemented in for example Firefox
0

You can exclude categories from your capture group

let regex = /(?:categories)+(\/\w+)/;
urls.forEach((str) => {
  console.log(str.match(regex)[1].replace('/',''));
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.