I'm doing some work with a chrome extension that needs to use the google chrome results URLs, which are redirects, and convert those to ordinary URLs for use.
Examples:
me Googling Google: https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&uact=8&ved=0ahUKEwi6w8WR4ZzSAhUZHGMKHRntAv0QFggaMAA&url=https%3A%2F%2Fwww.google.com%2F&usg=AFQjCNFePWT_Lkni-D9ikX7wC3eYuDMQYQ&sig2=gPD7xuE6nstkxWFhSh2QLQ&bvm=bv.147448319,d.cGw
What I want: https://www.google.com
I've tried using string.split().pop(), but that just used EVERYTHING after the inputted text. For a basic example, I want a way to reliably turn testHItestBYE into HIBYE. What would be the best way to do this?
Redirect URLs seem to have target URL in the url parameter, so we can split the string using '&url=' as delimiter, pop the last element, split it using '&' as the delimiter, get the first element. It is URL-encoded, so we'll have to decode it.
var findURL = function findURL(url){
return decodeURIComponent(url.split('&url=').pop().split('&')[0]);
}
//example call
result = findURL( 'https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&uact=8&ved=0ahUKEwi6w8WR4ZzSAhUZHGMKHRntAv0QFggaMAA&url=https%3A%2F%2Fwww.google.com%2F&usg=AFQjCNFePWT_Lkni-D9ikX7wC3eYuDMQYQ&sig2=gPD7xuE6nstkxWFhSh2QLQ&bvm=bv.147448319,d.cGw' );
console.log(result);
There are other ways to do it.
Related
I wrote a RegExp to grab and encode URLs in JavaScript.
This works fine but, it introduced a bug into my app.
I have a span Element which is used to display Emojis like this:
<span style="background:url(http://localhost/res/emo/face/E004.png)"></span>
Now, I'm using this Regular Expression to grab and convert anything URL into actual HTML clickable links:
/((https?:\/\/)?[\w-]+(\.[\w-]+)+\.?(:\d+)?(\/\S*)?)/ig
This ended up encoding the emoji URL into a clickable link.
Can anyone adjust that Code to Ignore URLs inside Elements or embedded Objects???
Please I need help!
This is the code:
var urlRegex = /((https?:\/\/)?[\w-]+(\.[\w-]+)+\.?(:\d+)?(\/\S*)?)/ig;
return txt.replace(urlRegex, function (url) {
var hyperlink = url;
if(!hyperlink.match('^https?:\/\/')) {
hyperlink = 'http://' + hyperlink;
}
return `${url}`;
});
I don't that the URLS inside
<span style="background:url(http://localhost/res/emo/face/E004.png)"></span>
were touched.
You would need to use negative look behind, which has limited support in JavaScript. (see here https://stackoverflow.com/a/50434875/6853740)
Simply adding negative look behind to your existing regex still doesn't work as expected:
((?<!url\()(https?:\/\/)?[\w-]+(\.[\w-]+)+\.?(:\d+)?(\/\S*)?) still matches "E004.png" in your example. Even other URL regexs from this post (What is the best regular expression to check if a string is a valid URL?) also match that. You may need to consider only looking for links that start with http:// or https:// which may help you recraft a regex that will only match full URLs.
I want to write a general pattern to be used in matching domain names with URLs. I have a case like the code below. The problem is that when I run the code, the browser freezes and I close it manually. The variable domain holds domain names which can be of the form: yahoo.com and also us.yahoo.com. The variable myString is a URL to be tested against the stored one. The test should be successful if the strings share the stored domain name, e.g. in the example below, the match will be -1 because the domain is google.co.uk while the string has: google.com. But I'm not getting -1 result. Instead, the program freezes. What could be the problem?
var domain="accounts.google.co.uk";
myString="https://accounts.google.com/ManageAccount";
var result=myString.search("(https:\/\/)(.*\.)*"+domain+"(\/.*)*(\/)*");
console.log(result);
EDIT:
Also tried:
var patt = new RegExp("(https:\/\/)(.*\.)*"+domain+"(\/.*)*(\/)*");
var result=patt.test(myString)
The same problem. The browser freezes and can't inspect code.
Since you're creating a RegExp from a string you need to escape the backslashes:
myString.search("(https:\\/\\/)(.*\\.)*"+domain+"(\\/.*)*(\\/)*")
I honestly don't know why it freezes instead of throwing an error or just failing to match properly.
There are lots of posts online like this, but none of them seem to do what I'm trying to do.
Let's say I have a domain in a string:
Extract hostname name from string
And I want to extract the domain name and nothing else (not the protocol, the subdomain or the file extension).
so for
https://stackoverflow.com/questions/8498592/extract-root-domain-name-from-string
I want to get:
stackoverflow.com
Is there any way to do this?
Try this on:
var url = 'http://stackoverflow.com/questions/8498592/extract-root-domain-name-from-string';
var domain = url.match(/^https?:\/\/([^\/?#]+)/)[1];
alert(domain);
This looks for a string that starts with http and optionally s, followed by ://, then matches everything it can that is not a /. But .match() returns an array here:
['http://stackoverflow.com', 'stackoverflow.com']
So, we use [1] to get the submatch.
You can use a simple regex like this:
\/\/(.*?)\/
Here you have a working example:
http://regex101.com/r/iP0uX7/1
Hope to help
I am looking to detect last url from text using javascript or mootools. Url canbe without prefix/scheme
I am working on URL auto sense like Facebook. Where a user may give an URL www.example.com or with http://www.example.com either of them should be detected by JavaScript. see stackoverflow detected URL that included with scheme without URL scheme it couldn't detect URL. In my case I need both.
Here is some text
'http://www.example.com www.example2.com'
Now I want www.example2.com It will be better if I get full array containing both http://www.example.com and www.example2.com
I searched a lot but couldn't find solution.
Most close to my requirements were Question about URL Validation with Regex and How do I extract a URL from plain text using jQuery?
Any help greatly appreciated.
by combing info in these 2 links:
How do I extract a URL from plain text using jQuery?
Detect URLs in text with JavaScript
We can get this:
http://jsfiddle.net/qQwGA/1/
If I understand what you're trying to do, this should cover it.
Given your input string, I think you just want to split it using spaces as separator?
.split(' ') ?
REGEX
/([^:\/?# ]+:)?(\/\/[^\/?# ]*)?[^?# ]+(\?[^# ]*)?(#\S*)?/gi
**SAMPLE CODE**
var str = 'http://www.example.com www.example2.com scheme://username:password#domain:port/path?query_string#fragment_id';
var t = str.match(/([^:\/?# ]+:)?(\/\/[^\/?# ]*)?[^?# ]+(\?[^# ]*)?(#\S*)?/gi);
/*
t contains :
[
"http://www.example.com",
"www.example2.com",
"scheme://username:password#domain:port/path?query_string#fragment_id"
]
*/
**DEMO**
>http://jsfiddle.net/wvYTd/
**DISCUSSION**
This regex will find any substring that looks like an URL in an input string.
No validation is performed on any URL found. For instance, if the input string is 3aBadScheme://hostname, the regex will detect it as an URL. In this example, 3aBadScheme is invalid since a scheme MUST start with a letter.
Excerpt from RFC3986
(...)Scheme names consist of a sequence of characters beginning with a letter and followed by any combination of letters, digits, plus ("+"), period ("."), or hyphen ("-").(...)
I have a Spring-MVC application with Freemarker as the view component.
In my templates, several links are generated which point back to my application and which include URL parameters containing a hash key (#).
Example:
parameter: Q#106368 11
URL generated by Freemarker with encoded param: testurl.html?key=Q%23106368%2011
I use JavaScript to redirect to this URL (reason: I use JS to manage loading of 2 frames at the same time).
The redirect method is simple:
function redir(url) {
window.location.href = url;
}
The JS call generated by Freemarker looks like
test
My problem is that the browser / Javascript converts back the URL encoded parameter, thinks there is a # and cuts off there.
When I use window.location.href='http://...' directly it works. Only when using the method parameter it seems to be magically URL decoded and then the redirect fails because the URL gets cut off at the #.
Is there an easy way to transmit the parameter correctly?
I am aware that I could replace the #, e.g. with $$$hash$$$, in the template and do the replacement on the server side again. But there are so many places I would have to change...
As Marc B commented, it is necessary to URL encode again. The method would be encodeURI(). However, this method does not encode the # sign. For my specific use case, I have to replace the # sign with %23 after the encoding.
The redirect JS method finally looks like:
function redir(url) {
url = encodeURI(url);
url = url.replace(/#/g, '%23');
window.location.href = url;
}
Comparing escape(), encodeURI(), and encodeURIComponent()
encodeURIComponent/decodeURIComponent is more thorough than just encodeURI, it will decode/encode '#' and event '/'
What browser are you using? I'm trying FireFox 5 and it doesn't convert %23 back into # in my testing. When you mouse over the link or copy the link location, what does that have? Are you sure whatever is outputting the link isn't doing the conversion?
Update
This isn't ideal, but it seems like it solves the problem:
<a onclick="url = 'http://localhost:8080/testappp/testurl.html?key=Q%23106368%2011';" href="javascript:redir(url);">test</a>
It seems like the href attribute is decoded. When I mouse over it I seen the # instead of the %23.