How to validate/block shorten URL in string - javascript

I need to block/validate shorten URL in String. Below string contains shorten URL how can I block/validate this in string .
Hi #first_name# This is Mondi from Novato Cleaners. May I ask for a favor ? Our google https://bit.ly requires reviews. Could you provide one ?Thank you

So for this you need to follow these steps:
1- extract all urls from string.
2- request each urls and get there original location. very well explained here:
How to get domain name from shortened URL with Javascript?
3- when you have originalUrl, just check if url != originalUrl then it is a shorten url.

Use regex to find whether there is a URL in your string or not, if they're just replacing it what you need on that space
/(https?://[^\s]+)/g
var string = "Hi Vignesh This is Mondi from Novato Cleaners. May I ask for a favor ? Our google https://bit.ly requires reviews. Could you provide one ?Thank you";
var protomatch = /(https?:\/\/[^\s]+)/g;
var b = string.replace(protomatch, '');
console.log(b)

Related

JS RegEx to remove part of a URL?

I am using the GoogleBooks API to search for particular titles by name and retrieve a cover image URL. For example, searching for "The Great Gatsby" will return the following image link:
http://books.google.com/books/content?id=HestSXO362YC&printsec=frontcover&img=1&zoom=1&edge=curl&source=gbs_api
If you look at the following image, you can see that there is a small fold on the bottom right corner. Some image URLs will have the fold and others won't. If you remove edge=curl from the URL link, the fold is removed.
Is there any way to use a regex to find and delete the curled portion?
Further, is there any way to use regex to change the img=1 value to img=2?
you can use the .replace() method
let URL = "some random URL you have"
console.log(URL.replace('&edge=curl',''))
Will replace every "&edge=curl" that it finds in this string and replace it with '' an empty string which is basically removing it.
You can also use the same method .replace() to replace any static URL variables like "img=1"
console.log(URL.replace('img=1','img=2'))
Don't use regex to parse URLs. Use URL object:
var u = new URL("http://books.google.com/books/content?id=HestSXO362YC&printsec=frontcover&img=1&zoom=1&edge=curl&source=gbs_api");
u.searchParams.delete("edge");
u.searchParams.set("img", "2");
console.log(u.href);
To obtain an updated url where the &edge=curl pattern is replaced and the &img= and &zoom= parameters are updated, you could achieve this by chaining multiple .replace() calls as shown below:
const url = "http://books.google.com/books/content?id=HestSXO362YC&printsec=frontcover&img=1&zoom=1&edge=curl&source=gbs_api"
// New values for img and zoom parameters
const img = 300;
const zoom = 22;
console.log(
url
.replace(/&img=(\w+)/,`&img=${img}`)
.replace(/&zoom=\w+/,`&zoom=${zoom}`)
.replace(/&edge=curl/,"")
)
Here the &img= and &zoom= parameters are updated with regular expressions &img=\w+ and &zoom=\w+, where \w+ will match one or more alpha numeric characters that appear after the parameter.
The advantage with this approach (over explicitly specifying img=1 and replacing it with img=2 ) is that you can update those parameter/value substrings of the input url without having to know the actual value of those parameters prior to replacement (ie that img has a value 1).
Note that this approach assumes the parameters being updated are prefixed with & (and not ?).
Hope that helps!
try this
let url = 'http://books.google.com/books/content?id=HestSXO362YC&printsec=frontcover&img=1&zoom=1&edge=curl&source=gbs_api';
// remove &edge=curl
url = url.replace('&edge=curl', '');
// replace img=1 with img=2
url = url.replace('img=1', 'img=2');

Get base url from string with Regex and Javascript

I'm trying to get the base url from a string (So no window.location).
It needs to remove the trailing slash
It needs to be regex (No New URL)
It need to work with query parameters and anchor links
In other words all the following should return https://apple.com or https://www.apple.com for the last one.
https://apple.com?query=true&slash=false
https://apple.com#anchor=true&slash=false
http://www.apple.com/#anchor=true&slash=true&whatever=foo
These are just examples, urls can have different subdomains like https://shop.apple.co.uk/?query=foo should return https://shop.apple.co.uk - It could be any url like: https://foo.bar
The closer I got is with:
const baseUrl = url.replace(/^((\w+:)?\/\/[^\/]+\/?).*$/,'$1').replace(/\/$/, ""); // Base Path & Trailing slash
But this doesn't work with anchor links and queries which start right after the url without the / before
Any idea how I can get it to work on all cases?
You could add # and ? to your negated character class. You don't need .* because that will match until the end of the string.
For your example data, you could match:
^https?:\/\/[^#?\/]+
Regex demo
strings = [
"https://apple.com?query=true&slash=false",
"https://apple.com#anchor=true&slash=false",
"http://www.apple.com/#anchor=true&slash=true&whatever=foo",
"https://foo.bar/?q=true"
];
strings.forEach(s => {
console.log(s.match(/^https?:\/\/[^#?\/]+/)[0]);
})
You could use Web API's built-in URL for this. URL will also provide you with other parsed properties that are easy to get to, like the query string params, the protocol, etc.
Regex is a painful way to do something that the browser makes otherwise very simple.
I know that you asked about using regex, but in the event that you (or someone coming here in the future) really just cares about getting the information out and isn't committed to using regex, maybe this answer will help.
let one = "https://apple.com?query=true&slash=false"
let two = "https://apple.com#anchor=true&slash=false"
let three = "http://www.apple.com/#anchor=true&slash=true&whatever=foo"
let urlOne = new URL(one)
console.log(urlOne.origin)
let urlTwo = new URL(two)
console.log(urlTwo.origin)
let urlThree = new URL(three)
console.log(urlThree.origin)
const baseUrl = url.replace(/(.*:\/\/.*)[\?\/#].*/, '$1');
This will get you everything up to the .com part. You will have to append .com once you pull out the first part of the url.
^http.*?(?=\.com)
Or maybe you could do:
myUrl.Replace(/(#|\?|\/#).*$/, "")
To remove everything after the host name.

How to get the main domain string using regular expression?

I have just started using regular expression and i landed up in a problem. So it would be really nice if someone can help me out with it.
The problem is, in case I have a url as given below;
$url = http://www.blog.domain.com/page/category=?
and want only the domain, how can i get it using regular expression in javascript.
thank you
This should work too, but most restrictive and shorter:
var url = "http://www.blog.domain.com/page/category"
var result = url.replace(/^(https?:\/\/)?(.+\.)*(([a-z0-9-]*)\.[a-z]{2,6})(\/.+)$/i,"$4")
If you want "domain.com" and not only "domain", use $3 instead of $4.
Explaination step by step:
A correct domain syntax: letters,numbers and "-" /([a-z0-9-]*)/i
Domain extension (2-6 chars): /(([a-z0-9-]*)\.[a-z]{2,6})/i
Subdomains: /(.+\.)*(([a-z0-9-]*)\.[a-z]{2,6})/i
An url start with http and maybe https: /^https?:\/\/(.+\.)*(([a-z0-9-]*)\.[a-z]{2,6})/i
You can put or not http when you type an url: /^(https?:\/\/)?(.+\.)*(([a-z0-9-]*)\.[a-z]{2,6})/i
Then what is after /: /^(https?:\/\/)?(.+\.)*(([a-z0-9-]*)\.[a-z]{2,6})(\/.+)$/i
Try below code
var url = "http://www.blog.domain.com/page/category=?";
var match = url .match(/(?:http?:\/\/)?(?:www\.)?(.*?)\//);
console.log(match[match.length-1]);
You can get it using the following RegEx: /.*\.(.+)\.[com|org|gov]/
You can add all of the supported domain extensions in this regex.
RegEx101 Explanation
Working Code Snippet:
var url = "http://www.blog.domain.gov/page/category=?";
var regEx = /.*\.(.+)\.[com|org|gov]/;
alert(url.match(regEx)[1]);
Do not use regex for this:
use hostname:
The URLUtils.hostname property is a DOMString containing the domain of
the URL.
var x = new URL("http://www.blog.domain.com/page/category=?").hostname;
console.log(x);
as pointed by vishwanath, URL faces compatibilty issues with IE<10 so for those cases, regex will be needed.
use this :
var str = "http://www.blog.domain.com/page/category=?";
var res = str.match(/[^.]*.(com|net|org|info|coop|int|co\.uk|org\.uk|ac\.uk|uk)/g);
console.log(res);
=> domain.com
the list in the regex can be expanded further depending upon your need.
a list of TLDs can be found here

RegExp - If first part of search string is found then replace with the full search string value

Is there a RegExp to find and replace a value based on the criteria, "if first part of search string is in the target string then replace the part that matches with the search string."
This is a special search and replace because the replacement is also used as the search string.
For example, I have this URL:
http://www.domain.com/path/something/more/something/
Search for any part of the following and replace with the whole:
/path/user/
Since, "/path/" is in both the replacement string and the target string the results would be:
http://www.domain.com/path/user/something/more/something/
NOTE: The search / replacement value can be anything.
I don't know what the replacement and search string is at the time I make a replacement so I can't use something that hard codes the search string. For example, this won't work because the term is hard coded:
s.replace(/(\/path\/)/, "$1value/");
Another example:
Here is the sentence, "Thank you Susan for your order."
Here is the search and replacement, "Susan Summers"
Here is the desired sentence, "Thank you Susan Summers for your order."
Use Case:
Lets say you are given 1 million text documents that are letters to customers but when they created the documents they used the customers first name only when they were supposed to use the full name. Now it's your job to find and replace every occurrence of their first name with their full name. You only have their full name to work with not first name.
Just realized this may not work as a RegEx and might require code.
You can use:
s = 'http://www.domain.com/path/something/more/something/';
r = s.replace(/(\/path\/)/, "$user/");
//=> "http://www.domain.com/path/user/something/more/something/"
You don't need to use regular expression for this case:
var url = 'http://www.domain.com/path/something/more/something/';
url.replace('/path/', '/path/user/');
// => "http://www.domain.com/path/user/something/more/something/"
I'm not quite sure if I understand the problem correctly. The following replaces any part of of /path/user/ (-> part 1: 'path', part 2: 'user') with the whole /path/user:
var url1 = "http://www.domain.com/path/something/more/something/";
var url2 = "http://www.domain.com/user/something/more/something/";
url1.replace(/\/path\/|\/user\//, '/path/user/');
url2.replace(/\/path\/|\/user\//, '/path/user/');
results in:
http://www.domain.com/path/user/something/more/something/
http://www.domain.com/path/user/something/more/something/
I hope this is what you need, otherwise, please add another example.
EDIT:
Here is the regex in action: http://regex101.com/r/jL6tK6
split + join alternative :
url = url.split('/path/').join('/path/user/');
Although your requirements are not clear, here is a guess that raises a few extra questions :
var sub = '/path/user/';
var parts = sub.match(/[^\/]+/g);
url = url.replace(new RegExp(
'\\/(' + [parts.join('\\/')].concat(parts).join('|') + ')\\/'
), sub);
The resulting regular expression is as follows :
/\/(path\/user|path|user)\// // "/path/user/" OR "/path/" OR "/user/"
Let's check some urls assuming we live in the best of worlds :
'http://domain/' -> 'http://domain/'
'http://path/user/' -> 'http://path/user/'
'http://path/' -> 'http://path/user/'
'http://user/' -> 'http://path/user/'
Now, what do you think about the following ones?
'http://path/user' -> 'http://path/user/user'
'http://user/path/' -> 'http://path/user/path/'
'http://path/user/path/' -> 'http://path/user/path/'
The remaining questions are :
Is this what you are looking for?
What to do when there is no trailing slash?
What to do in the reverse order case?
What to do with recurrent parts?

How to pull a unknown URL out of a String

I'm writing a Node/Express app and I have a text string in a JSON object that I need to pull a URL out of. The URL is different every time, and the string itself has two very similar URL's, and I only want to pull out one.
The only thing I do know is that in the string, the url will always be preceded with the same text.
String:
The following new or updated things match your search criteria.
Link I Need
<http://randomurl.com/Junk/Yay/ThisView.aspx?r=164241242186&s=J
WD&t=JWD>
Link I don't Need
<http://randomurl.com/Junk/Yay/ThisView.aspx?r=164241242186&s=J
WD&t=JWD&m=true>
Search was last updated on April 12th, 2013 # 14:43
If you wish to unsubscribe from this update...
Out of this string all I need to pull out is the URL under Link I Need, http://randomurl.com/Junk/Yay/ThisView.aspx?r=164241242186&s=J
WD&t=JWD and nothing else. I'm not quite sure how to go about this, any help would be greatly appreciated!
Something like this should work:
var s = "The following new or updated ...";
var regex = /Link I Need\s*<([^>]*)>/;
var match = s.match(regex);
var theUrl = match && match[1];
This assumes that the URL is not split across newlines. If it is, then after you find the match, you need to to
theUrl = theUrl.replace(/\s+/, '')

Categories

Resources