Javascript regular Expressions cut off last parameters between slashes - javascript

Hi I'm new to regex and can't find a proper solution for this:
I want to cut off the last parameters of the url between the two slashes "/1032/" form url "http://www.blablabla.com/test.php/addpage/1032/"
That i have just http://www.blablabla.com/test.php/addpage/ ...though this part is not important of being matched...so just cut off the parameters between the last slashes...
What i did was:
curr_url= "http://www.blablabla.com/test.php/addpage/1032/";
expression =/.*\/./;
alert(expression.exec(curr_url));
Result is "http://www.blablabla.com/test.php/addpage/1"
Now i could cut off the last parameter by a slice but thats not reasonable i guess
Any better solutions? Thanks a lot!

curr_url.match(/.+(\/.+\/)$/)
["http://www.blablabla.com/test.php/addpage/1032/", "/1032/"]
First greedy .+ captures everything to last slash and then backs off to next-to-last, because rest of pattern would fail otherwise. () capture everything between this slash and last an then \/$ at the end tell that string should end after last slash. Move slashes outside brackets if you only want number itself.
It seems though I misinterpreted your intent. If you need first part of string, you can use regexp suggested bellow with a little change:
curr_url.match(/(.+)\/.+/)[1]
It captures "everything until slash, and then some more". It will first reach last slash in string, but then back off to previous slash, because there's not "then some more", thus leaving exactly the part of string you want.

var parts=curr_url.split('/');
After that you can get any part from the array.

In your expression
/.*\/./
you need the last dot to tell the pattern that there is a character after the slash following, but this is also matched and therefore in your result. If you remove it it will match till the last slash, so no solution.
But there is a way to define what should be after the pattern and to not match that stuff. Its called a look ahead assertion.
So you could do
/.*\/(?!$)/
and (?!$) is a negative lookahead assertion that means "Match the previous slash only if the end of the string (the $) is not following".
curr_url= "http://www.blablabla.com/test.php/addpage/1032/";
expression =/.*\/(?!$)/;
alert(expression.exec(curr_url));
Would return also what you want.

Related

How to extract separate parts of a string with a regex

I'm trying to build a regex that can process the following:
abc
abc-def
where the -def part is optional.
I'm wanting to get capture groups for the "abc", and optional "def" part.
I've tried this (in Javascript) but can't seem to figure out the optional part:
/^(.*)+(-(.*))?$/
It matches both examples but the optional part is contained in the first capture group. This should be simple, but I can't seem to get it right.
You're close, try a ? to make the expression lazy.
/^(.*?)(-(.*))?$/
You can try /^([^-]+)(-(.*))?$/. One issue is that the first + is outside of the capture group which means it'll only match the last character. Secondly, the .* is greedy and will match a -, gobbling all the way to the end of the line.
Runnable example:
console.log("abc-def".match(/^([^-]*)(-(.*))?$/));
console.log("abc".match(/^([^-]*)(-(.*))?$/));
You may not need to capture the substring starting with -, in which case /^([^-]*)(?:-(.*))?$/ could work.

How can I match the last part of an email via JavaScript? [duplicate]

Using a regular expression (replaceregexp in Ant) how can I match (and then replace) everything from the start of a line, up to and including the last occurrence of a slash?
What I need is to start with any of these:
../../replace_this/keep_this
../replace_this/replace_this/Keep_this
/../../replace_this/replace_this/Keep_this
and turn them into this:
what_I_addedKeep_this
It seems like it should be simple but I'm not getting it. I've made regular expressions that will identify the last slash and match from there to the end of the line, but what I need is one that will match everything from the start of a line until the last slash, so I can replace it all.
This is for an Ant build file that's reading a bunch of .txt files and transforming any links it finds in them. I just want to use replaceregexp, not variables or properties. If possible.
You can match this:
.*\/
and replace with your text.
DEMO
What you want to do is match greedily, the longest possible match of the pattern, it is default usually, but match till the last instance of '/'.
That would be something like this:
.*\/
Explanation:
. any character
* any and all characters after that (greedy)
\/ the slash escaped, this will stop at the **last** instance of '/'
You can see it in action here: http://regex101.com/r/pI4lR5
Option 1
Search: ^.*/
Replace: Empty string
Because the * quantifier is greedy, ^.*/ will match from the start of the line to the very last slash. So you can directly replace that with an empty string, and you are left with your desired text.
Option 2
Search: ^.*/(.*)
Replace: Group 1 (typically, the syntax would be $1 or \1, not sure about Ant)
Again, ^.*/ matches to the last slash. You then capture the end of the line to Group 1 with (.*), and replace the whole match with Group 1.
In my view, there's no reason to choose this option, but it's good to understand it.

JavaScript regex to capture everything after the second to last backslash

I have a regex that looks like the following currently:
/^.*[\\\/]/
This will strip every single backslash from a string. The problem I'm facing is I have to now be able to capture everything from the second to last backslash.
Example:
/Users/foo/a/b/c would return b/c
/Another/example/ would return Another/Example
So I need to capture everything after the second to last backslash. How would the regex above do that?
Try with this simple solution:
s = "aaaa/bbbb/cccc/dddd";
s.split("/").slice(-2).join("/"); /* It will return "cccc/dddd" */
I assume that you mean forward slash, not backslash.
Here is a regex alternative to pierlauro's answer.
/([^\/]+\/[^\/]+)\/?$/
Regex101
As pierlauro's answer shows, split, join, and slice are probably the best options for this. But if you MUST use a regex (not sure why), you could employ something like the following:
\/?(\[^\/\]+?\/\[^\/\]+)\/?$
This regex accommodates for optional trailing slashes and for urls shorter than 2 /s. It leverages the $ character to focus our search scope on the end of the string.

Unable to find a string matching a regex pattern

While trying to submit a form a javascript regex validation always proves to be false for a string.
Regex:- ^(([a-zA-Z]:)|(\\\\{2}\\w+)\\$?)(\\\\(\\w[\\w].*))+(.jpeg|.JPEG|.jpg|.JPG)$
I have tried following strings against it
abc.jpg,
abc:.jpg,
a:.jpg,
a:asdas.jpg,
What string could possible match this regex ?
This regex won't match against anything because of that $? in the middle of the string.
Apparently using the optional modifier ? on the end string symbol $ is not correct (if you paste it on https://regex101.com/ it will give you an error indeed). If the javascript parser ignores the error and keeps the regex as it is this still means you are going to match an end string in the middle of a string which is supposed to continue.
Unescaped it was supposed to match a \$ (dollar symbol) but as it is written it won't work.
If you want your string to be accepted at any cost you can probably use Firebug or a similar developer tool and edit the string inside the javascript code (this, assuming there's no server side check too and assuming it's not wrong aswell). If you ignore the $? then a matching string will be \\\\w\\\\ww.jpg (but since the . is unescaped even \\\\w\\\\ww%jpg is a match)
Of course, I wrote this answer assuming the escaping is indeed the one you showed in the question. If you need to find a matching pattern for the correctly escaped one ^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))+(\.jpeg|\.JPEG|\.jpg|\.JPG)$ then you can use this tool to find one http://fent.github.io/randexp.js/ (though it will find weird matches). A matching pattern is c:\zz.jpg
If you are just looking for a regular expression to match what you got there, go ahead and test this out:
(\w+:?\w*\.[jpe?gJPE?G]+,)
That should match exactly what you are looking for. Remove the optional comma at the end if you feel like it, of course.
If you remove escape level, the actual regex is
^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))+(.jpeg|.JPEG|.jpg|.JPG)$
After ^start the first pipe (([a-zA-Z]:)|(\\{2}\w+)\$?) which matches an alpha followed by a colon or two backslashes followed by one or more word characters, followed by an optional literal $. There is some needless parenthesis used inside.
The second part (\\(\w[\w].*))+ matches a backslash, followed by two word characters \w[\w] which looks weird because it's equivalent to \w\w (don't need a character class for second \w). Followed by any amount of any character. This whole thing one or more times.
In the last part (.jpeg|.JPEG|.jpg|.JPG) one probably forgot to escape the dot for matching a literal. \. should be used. This part can be reduced to \.(JPE?G|jpe?g).
It would match something like
A:\12anything.JPEG
\\1$\anything.jpg
Play with it at regex101. A better readable could be
^([a-zA-Z]:|\\{2}\w+\$?)(\\\w{2}.*)+\.(jpe?g|JPE?G)$
Also read the explanation on regex101 to understand any pattern, it's helpful!

Regex keeps trailing slash in capture group

So, I've tried making a little Regex expression to fetch the requested URL, minus the starting an trailing slash.
One little catch, the trailing slash wont always be there, for instance if the user requests "/test/example/", they can also request "/test/example". So I tried to make a method to handle that:
req.url.match(/^(?:\/)(.+)(?:[\/])?$/i)[1]
Although, if I request a path like "/test/example/", it keeps the trailing slash, and returns "test/example/" in the capture group...? Basically what I wanted to avoid. (So, all it's doing is removing the starting slash)
Now, I tried removing the ? that's next to the $ symbol. But this just causes an error when requesting "/test/example" (something without the trailing slash), because [1] would be null.
I made an example on regex101, which you can view here. As you can see, the capture group includes the ending slash, even though in my expression, I thought I told it to not do that.
TL;DR: Regex is still capturing trailing slash, even though I don't want to do (and keep in mind that the trailing slash wont always be present).
To clairify, I want the regex to do this:
"/test/example/" to "test/example"
and
"/test/example" to "test/example"
(So, removing the starting and trailing slash, but the trailing slash is optional)
You need to make the regex less greedy. Add 2 ?s:
^(?:\/)?(.*?)(?:[\/])?$
See updated regex here.
use this pattern and replace with nothing
^\/|\/$
Demo
Javascript's regex engine is not the strongest.
I had an issue just like this and I ended up doing the regex in two steps for better readability and predictability.
var matches = req.url.match(/^(?:\/)(.+)(?:[\/])?$/i)
if(matches.length){
var my_url = matches[1].replace(/(^\/|\/$)/g,'') // Removes start and ending slashes
}else{
var my_url = 'something_else'
}

Categories

Resources