Regular expression questions.

Asked 1 years ago, Updated 1 years ago, 69 views

I'm checking the uri string, but it's really hard to understand even if I see a lot of regular expressions;;;

So I have a question. Help!

The conditions are as follows.

If '/' appears in the last character, false

/api/users > false

/api > true

/api?id=asfdasjlk&asdf=asdf > true

As far as I've tried, /api/users is false, /api is also false, it should be true,

I think I should write an If statement inside the regular expression, but it's frustrating

/\/api\/users[a-zA-Z0-9^\/]/.test('/api/users')

regex

2022-09-22 10:47

3 Answers

How about this side?

\/api(?!\/users).*[^\/]$

(.*[^\/]+) instead of negate lookbehind?Inserted $. It can also be used in JavaScript.

The regular expression itself can be further reduced, so we modified it (.*[^\/])?$You said you didn't understand this part, so I'll add an explanation.

The previous \/api(?!\/users) can satisfy /api = True, /api/users = False. Other conditions to be satisfied are

It is.

First of all, the regular expression that I wrote.*[^\/]$ is broken and explained as follows.

If you combine them, "It's okay to get any string regardless of length ().*), \ does not end ([^\/]$).


2022-09-22 10:47

I thought it would be true if /api was included.

\/api(?!\/users).*(?<!\/)$

You can squeeze it like this


2022-09-22 10:47

/\/api(?!\/users).*(?<!\/)$/ I hit the console window as it is, and an error appears. What does < mean in number 3?

Looking back, the conditions for the question were strange.

`/api` 
// // true

`/api?id=1234`
// // true

`/api/`
// // false

`/api/users` 
//  //  false


2022-09-22 10:47

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.