ExpressJS route regex - regex

I have route:
app.get('/:id', routes.action);
It works fine, but I need skip robot.txt and other (humans ....)
I create regex (only chars or number):
/^[a-z]{0,10}$/
How I can route only ids, which match this regex?

Put the regex in parentheses like this:
app.get('/:id(^[a-z]{0,10}$)', routes.action);

If you want to avoid a route matching a static file that exists physically, simply put the static middleware before the call to the app.router.
Then the static file (such as robots.txt) will be delivered and these calls will not get through to your routing.
Problem solved ;-).

Internally, the strings that you give to the Express router are just converted into regexes anyway. If you look at the code, you can see that you can just pass a regex directly.
app.get(/^\/[a-z]{0,10}$/, routes.action);
The docs also have examples.

If you need it for multiple routes :
app.routes.get
is an array having all of the get routes.
You can change the regex object for the routes you need to change.

Related

REGEX: find URL with specific words/pages

I have the current regex exp:
http[s]?://(?:[a-zA-Z]|[0-9]|[$-_#.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+
Which retrieves all the urls from a file, but I need it to only get the urls with a specific page, let's say page-to-find and I can't seem to do it without having the expression to add to a second group and I only want it natively in one group instead of two, as direct as possible.
Any tips?
Thanks
If its a page what does it end in? .asp? .php? .aspx? .htm? .html? (Something else?)
Try this for a start:
http[s]?://.*page-to-find

CodeIgniter Route - regex matches everything

I haven't been coding with CI for long and I want to rewrite some of the routes I previously had a bit more dynamically.
For some reason, this regex I added matches everything and I cannot figure out why.
$route['([a-z]+-[a-z]+)'] = 'Main/example_function/$1';
It matches all other routes that haven't been assigned yet, even with more than one fragment. Examples of routes it matches:
example.com/aaaaa
example.com/aaaaa-asd
example.com/aaaaa/aaaa
I only need it to match specifically routes of this style:
example.com/aaaaa-aaaaa
I have other regexps in different routes, but of the form example.com/test/([a-z]+) and they work fine.
Putting it after all other declared routes is not an option because not all routes are declared there.
Thanks in advance!
Do you need this...
Regex: ^example\.com\/[a-z]+\-[a-z]+$
Regex demo
Thanks to Sahil Gulati answer, I found out the error. The correct regex is:
$route['([a-z]+\-[a-z]+)'] = 'Main/example_function/$1';
And it would correctly match what I wanted
Thanks!

Regex to look for url start value and end value

I'm using using regex to look for URL that starts with http or https and with a specific value.
^http|https\:\/\/www
This regex looks at the http/https in a URL and this works.
/[\/]\bvalue?\b[\/]/g
This regex looks for "value" in a url and this currently matches with
http://www.test.co.uk/value/
http://www.test.co.uk/folder/value/
Is there a possibility to put those two regex together? Basically I need to display URLs that doesn't contain http/https or /value/ in the URL path
You're looking to do this: /(?=^(https|http))|(\bvalue\b)/g
First half: (?=^(https|http)) which will look first for https and then for http. My personal opinion however is to reduce the code to look only for http, since by matching for http you can also match for https. You may think this behavior is not going to work, but logically it does. You can try that if you like and see what happens.
Second half: (\bvalue\b). You can be more specific such as it being between forward and back slashes, or not. I used the \b delimiter to avoid it being part of another string and it worked quite well.
The important part here is to unite them, so use the | operator and it yields the above result.
Test strings:
http://www.helloworldvalue/value/values/
https://www.helloworldvalue/values/svalue/value/value/vaaluevalue/
Try it and let me know if you have any questions in the comments below.

Angular UI Router Regex for non-stateParams segements

I have 2 URL patterns that I want to target the same state:
/:client/:project/deliverables/resources_reports/:id/resources/:rsrc_id
/:client/:project/deliverables/issues_exports/:id/resources/:rsrc_id
I tried making a single state to manage this using regex to no avail. I'm not sure if the regexp is only applicable to the $stateParam keys in those URLs or not. Is there a way to make it something like this:
url: "/:client/:project/deliverables/(resources_reports|issues_exports)/:id/resources/:rsrc_id"`
Thank you in advance :)
I was missing one thing: it has to be set within a Url matcher's matching segment. The unexpected result is that you get another $stateParams property to utilize.
/:client/:project/deliverables/{d_type:resources_reports|issues_exports}/:id/resources/:rsrc_id

Regex to match url but not urlMvc

I'm going through my Android app at the minute making sure all my HTTP calls point to the same place. I want to run a search so I don't have to manually look through each file and possibly miss one. I've seen I can do a find using Regex. What I need is a url that matches the string url but ignores urlMvc and urlProcedural (as there the variables the calls should be made to). Is this something thats possible with a Regex or will I have to go through all the files manually?
Copy from comment: You can use negative lookahead: url(?!Mvc|Procedural)