Window Location Pathname with Multiple Directories - javascript

I'm sure this very easy, but I am having trouble writing this path correctly. I have no problems triggering the command using:
if(window.location.pathname.match(/^\/Home-Blog/)) {$('#maincolumn').remove();}
But for another case, I need the pathname to be longer, with the following directory:
/Home-Blog/CategoryBlogID
Any way I try to insert it, it doesn't seem to work. How do I plug in the path to "CategoryBlogID"?
Thank you!

I'm not totally sure I understand your question, but if you want to match
/Home-Blog
/Home-Blog/
/Home-Blog/CategegoryBlogID
this RegExp should do the trick:
"/Home-Blog/CategoryBlogIDs".match(/^\/Home-Blog(\/)*(CategoryBlogID)?$/)
/^ start of string
\/ a forward slash
Home-Blog the text "Home-Blog"
(\/)* a forward slash zero or more times (will also match /Home-Blog////CategoryBlogID) because of this
(CategoryBlogID)? the string "CategoryBlogID" zero or one times
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp#grouping-back-references

Related

Regex to find web addresses in short copy

Having a short copy I need to match all occurrences of links to websites. To keep things simple a need to find out addresses in this format:
www.aaaaaa.bbbbbb
http://aaaaaa.bbbb
https://aa.bbbb
but also I need to take care of longer www/http/https versions:
www.aaaaa.bbbb.ccc.ddd.eeee
etc. So basically number of subdomains is not known. Now I came up with this regex:
(www\.([a-zA-Z0-9-_]|\.(?!\s))+)[\s|,|$]|(http(s)?:\/\/(?!\.)([a-zA-Z0-9-_]|\.(?!\s))+)[\s|,|$]
If you test on:
this is some tex with www.somewIebsite.dfd.jhh.hjh inside of it or maybe http://www.ssss.com or maybe https://evenore.com hahaah blah
It works fine with exception of when address is at the very end. $ seems to work only when there is \n in the end and it fails for:
this is some tex with www.somewIebsite.dfd.jhh.hjh
I'm guessing fix is simple and I miss something obvious so how would I fix it? BTW I posted regex here if yu want to quickly play around https://regex101.com/r/eL1bI4/3
The problem is that you placed the end anchor $ inside the character group []
[\s|,|$]
It is then interpreted literally as a dollar sign, and not as the anchor (the pipe character | is also interpreted literally, it's not needed there). The solution is to move the $ anchor outside:
(?:[\s,]|$)
However, in this case it makes more sense to use a positive lookahead instead of the noncapturing group (you don't want trailing spaces, or commas):
(?=[\s,]|$)
In the result you will end up with the following regex pattern:
(www\.([a-zA-Z0-9-_]|\.(?!\s))+)(?=[\s,]|$)|(http(s)?:\/\/(?!\.)([a-zA-Z0-9-_]|\.(?!\s))+)(?=[\s,]|$)
See the working demo.
The updated version that handles trailing full stops:
(www\.([a-zA-Z0-9-_]|\.(?!\s|\.|$))+)(?=[\s,.]|$)|(http(s)?:\/\/(?!\.)([a-zA-Z0-9-_]|\.(?!\s|\.|$))+)(?=[\s,.]|$)
See the working demo.

RegExp for matching protocol relative URL at beginning of string

I'm writing a short dynamic script piece that makes sure a URL starts with either:
http
https
no http/https, and merely just the // technique
And my RegExp so far:
/^(http|https|://)youtube.com|vimeo.com.*$/
So I could give it:
http://youtube.com
https://youtube.com
//youtube.com
... And it would work
I know it's nearly there, but how can I get it to say "http/https or just slash slash" at the beginning of the string? And then whatever comes after those URLs.
If you would like to know what's wrong with your regex, here's an explanation:
/^(http|https|://)youtube.com|vimeo.com.*$/
First thing's first: You need to encapsulate the youtube|vimeo part as a group (we'll add ?: to the front since we don't want to capture it). otherwise it's actually looking for (^(http|https|://)youtube.com)|(vimeo.com) which is not what you intend at all. Also, you'll want to escape your periods, since that is the dotall character (I'm assuming you do not want "youtubescom" to match), as well as your forward slashes.
Now, we have:
/^(http|https|:\/\/)(?:youtube\.com|vimeo\.com).*$/
So, here we're checking to see if "http", "https" or "://" starts a string and comes before either "youtube.com" or "vimeo.com". But, what we really want is to check if "http://", "https://" or just "//" comes before either:
/^(http:\/\/|https:\/\/|\/\/)(?:youtube\.com|vimeo\.com).*$/
You can stop there, that's your answer. However, let's continue with some cleanup by finding the redundancies. First, both our domains both have ".com", so we can make that part simply (?:youtube|vimeo)\.com. Next, our protocol prefixes all have "//", so we can pull that out to : (http:|https:)\/\/. However, now "http:" or "https:" must start the string. Since we want those to be optional, we'll add a "?" afterwards, so we get (http:|https:)?\/\/. Now, since those are so close, all we really want is that optional "s" for ssl. Finally, we can finally get:
/^(https?:)?\/\/(?:youtube|vimeo)\.com.*$/
You can try this regex:
/^(https?:)?\/\/(?:youtube|vimeo)\.com.*$/i

Javascript match eveything except given words

Im working on a node.js app, and im doing router matching.
I need to match all routes with all variables except the ones which begin with
"public , static , files or same words with added "/"
i know i could do it using an if statement before regexp, to check if those words are withing url, and if they are, skip regexp, but i dont want to add such nesting, and knowing how to do it using regexp will become in handy in the future anyways.
i know how to match anything except...some letters, ie ^[0-9] , but i cant use the same for words. I googled and found that lookahead could solve this, but... i cant get it to work.
In the end, id like to use something like this (in pseudo code)
where the .+ would match only if the pattern does not match any of the given words.
match(/^(?!public|static|files) .+ /gi)
edit 1:
The format of the url's would be something like this..with or without slashes.
/controller/action/4/var:something/
i want to make a regexp that matches this controller - action - id
pattern, but at the same time wouldnt match patterns like this
/public/images/4
or
static/files/somefile
in general, id like to know how to match a pattern, but only if it doesnt begin with given words.
e.g something like this...but it doesnt work
( match .+, but only if it doesnt contain the words mentioned before
/^(?!public|static|files).+ /gi)
Actually, I'm not having trouble with negative look-aheads. Something like this seems to work just fine, although it's not super extensible.
/^\/(?!public|static|files)([^\/]+)?\/?([^\/]+)?\/?([^\/]+)?\/?(.*)$/i
1st capture will be the controller, 2nd is the action, 3rd is the ID, and 4th is whatever is left.
See this jsfiddle

Only match regex if it doesnt start with a pattern in javascript

I have a bit of a strange one here, I basically have a large chunk of text which may or may not contain links to images.
So lets say it does I have a pattern which will extract the image url fine, however once a match is found it is replaced with a element with the link as the src. Now the problem is there may be multiple matches within the text and this is where it gets tricky. As the url pattern will now match the src tags url, which will basically just enter an infinite loop.
So is there a way to ONLY match in regex if it doesnt start with a pattern like ="|=' ? as then it would match the url in something like:
some image http://cdn.sstatic.net/stackoverflow/img/sprites.png?v=6
but not
some image <img src="http://cdn.sstatic.net/stackoverflow/img/sprites.png?v=6">
I am not sure if it is possible, but if it is could someone point me in the right direction? A replace by itself will not suffice in this scenario as the url matched needs to be used elsewhere too so it needs to be used like a capture.
The main scenarios I need to account for are:
Many links in one block of varied text
A single link without any other text
A single link with other varied text
== edit ==
Here is the current regex I am using to match urls:
(\b(https?|ftp|file):\/\/[-A-Z0-9+&##\/%?=~_|!:,.;]*(?:png|jpeg|jpg|gif|bmp))
== edit 2 ==
Just so everyone understands why I cannot use the /g command here is an answer which explains the issue, if I could use this /g like I originally tried then it would make things a lot simpler.
Javascript regex multiple captures again
What you are looking for is a negative look behind, but Javascript doesn't support any kind of look behinds, so you will either have to use a callback function to check what was matched and make sure it is not preceded by a ' or ", or you can use the following regex:
(?:^|[^"'])(\b(https?|ftp|file):\/\/[-a-zA-Z0-9+&##\/%?=~_|!:,.;]*(?:png|jpeg|jpg|gif|bmp))
which has a single problem, that is in the case of a successful match it will catch one more character, the one right before the (\b(https?|ftp|file) pattern in the input, but I think you can deal with this easily.
Regex101 Demo
Using the /ig command at the end should work... the g is for global replace and the i is for case-insensitivity, which is necessary as you've only got A-Z instead of a-zA-Z.
Using the following vanilla JS appears to work for me (see jsfiddle)...
var test="some image http://cdn.sstatic.net/stackoverflow/img/sprites.png?v=6 some image http://cdn.sstatic.net/stackoverflow/img/sprites.png?v=6 some image http://cdn.sstatic.net/stackoverflow/img/sprites.png?v=6";
var re = new RegExp(/(\b(https?|ftp|file):\/\/[-A-Z0-9+&##\/%?=~_|!:,.;]*(?:png|jpeg|jpg|gif|bmp))/ig);
document.getElementById("output").innerHTML = test.replace(re,"<img src=\"$1\"/>");
Although, what it does highlight is that the query string part of the URL (the ?v=6 is not being picked up with your RegEx).
For jQuery, it would be (see jsfiddle)...
$(document).ready(function(){
var test="some image http://cdn.sstatic.net/stackoverflow/img/sprites.png?v=6 some image http://cdn.sstatic.net/stackoverflow/img/sprites.png?v=6 some image http://cdn.sstatic.net/stackoverflow/img/sprites.png?v=6";
var re = new RegExp(/(\b(https?|ftp|file):\/\/[-A-Z0-9+&##\/%?=~_|!:,.;]*(?:png|jpeg|jpg|gif|bmp))/ig);
$("#output").html(test.replace(re,"<img src=\"$1\"/>"));
});
Update
Just in case my example of using the same image URL in the example doesn't convince you - it also works with different URLs... see this jsfiddle update
var test="http://cdn.sstatic.net/stackoverflow/img/sprites.png?v=6 http://cdn.sstatic.net/serverfault/img/sprites.png?v=7";
var re = new RegExp(/(\b(https?|ftp|file):\/\/[-A-Z0-9+&##\/%?=~_|!:,.;]*(?:png|jpeg|jpg|gif|bmp))/ig);
document.getElementById("output").innerHTML = test.replace(re,"<img src=\"$1\"/>");
Couldn't you just see if there is a whitespace in front of the url, instead of that word-boundary? seems to work, although you will have to remove the matched whitespace later.
(\s(https?|ftp|file):\/\/[-A-Z0-9+&##\/%?=~_|!:,.;]*(?:png|jpeg|jpg|gif|bmp))
http://rubular.com/r/9wSc0HNWas
Edit: Damn, too slow :) I'll still leave this here as my regex is shorter ;)
as was said by freefaller, you might use /g flag to just find all matches in one go, if exec is not a must.
otherwise: you can add (="|=')? to the beginning of your regex, and check if $1 is undefined. if it is undefined, then it was not started with a ="|=' pattern

Regex in JavaScript

Suppose we don't know how many slashes we could get in a string but we do not want any extra slashes. So if we get this string '/hello/world///////how/are/you//////////////' we should transform it to the form of '/hello/world/how/are/you/'. How to do it with the help of regular expressions in JavaScript?
"/hello/world///////how/are/you//////////////".replace(/\/+/g, "/")
'/hello/world///////how/are/you//////////////'.replace(/\/{2,}/g, '/');
This might be an incy wincy bit faster than mkoryak's suggestion, for it will only replace where necessary – i.e., where there's multiple instances of /. I'm sure someone with a better understanding of the nuts and bolts of the JavaScript regular expression engine can weigh in on this one.
UPDATE: I have now profiled mine and mkoryak's solutions using the above string but duplicated hundreds of times, and I can confirm that my solution consistently worked out several milliseconds faster.
Edited: mkoryak's answer below is way better. Leaving this in case the info it contains is useful for someone else.
You could capture each word + slash group and look ahead (but don't capture) for one or more extra slash. Like...
(\w+\/)(?:\/)*(\w+\/)(?:\/)*
First () captures one or more of any word character followed by a slash, second () looks for a slash but doesn't capture, * means find 0 or more of the proceeding token. Etc.
Hope that helps!
I want to make a regex for string which matches from point A till point B
text= "testtttExecuted 'show bootvar' on \n10.238.196.66. kjdkhfkh Executed tsttt\n fhgkhkh"
Output should be
testtttExecuted 'show bootvar' on \n10.238.196.66. kjdkhfkh
I want to make a regex for string which matches from point A till point B
text= "testtttExecuted 'show bootvar' on \n10.238.196.66. kjdkhfkh Executed tsttt\n fhgkhkh"
Output should be
testttt<font color='red'>Executed 'show bootvar' on \n</font>10.238.196.66. kjdkhfkh <font color='red'>Executed tsttt\n</font> fhgkhkh

Categories

Resources