How to get the directory from an URL using JS? - javascript

I have one URL which is
https://hpscrmdev.honeywell.com/service_enu_dv2_oui/start.swe?SWECmd=GotoView&SWEView=Home+Page+View+(WCC)&SWERF=1&SWEHo=hpscrmdev.honeywell.com&SWEBU=1
I want this 'service_enu_dv2_oui' part using regex. How can I get that?
Please suggest.
Thanks.

this is without regex but working nicely: http://jsfiddle.net/mKNkF/
var path = "https://hpscrmdev.honeywell.com/service_enu_dv2_oui/start.swe?SWECmd=GotoView&SWEView=Home+Page+View+(WCC)&SWERF=1&SWEHo=hpscrmdev.honeywell.com&SWEBU=1";
var pathArray = path.split( '/' );
alert(pathArray[3]);

If the path only has a single segment as shown in your example, the regex is fairly straight-forward:
/^http(?:s?):\/\/[^\/]+\/([^\/]+)/
Live link with details.
For example:
var str = "https://hpscrmdev.honeywell.com/service_enu_dv2_oui/start.swe?SWECmd=GotoView&SWEView=Home+Page+View+(WCC)&SWERF=1&SWEHo=hpscrmdev.honeywell.com&SWEBU=1";
var match = str.match(/^http(?:s?):\/\/[^\/]+\/([^\/]+)/);
if (match) {
console.log(match[1]); // "service_enu_dv2_oui"
}

Path without host and query you can receive from
window.location.pathname
http://www.w3schools.com/js/js_window_location.asp
No regex required

https?:\/\/[\w\.]+\/(\w+)\/
this regex have one match group
you check this link

Related

Remove particular string from url Jquery

I need to remove size i.e. resolution of image path from url. Take following example:
http://raudevlocal.com/wp/wp-content/uploads/2017/05/0000362_chocolate-layer-cake-1024x682.jpeg
I need to remove -1024x682
http://raudevlocal.com/wp/wp-content/uploads/2017/05/rohit-300x118.jpg
Need to remove -300x118
use regex pattern
$(document).ready(function(){
window.location.href = window.location.href.replace(/(-\d{2-6}x\d{2-6})/g,'');
});
str = "http://raudevlocal.com/wp/wp-content/uploads/2017/05/0000362_chocolate-layer-cake-1024x682.jpeg";
str.replace(/-(\d+x\d+)/, "");
output will be
http://raudevlocal.com/wp/wp-content/uploads/2017/05/0000362_chocolate-layer-cake.jpeg
use regular expression
var string = "http://raudevlocal.com/wp/wp-content/uploads/2017/05/0000362_chocolate-layer-cake-1024x682.jpeg";
console.log(string.replace(/-\d+x\d+/g,''));
window.loaction.href= window.location.href.replace(/-\d+x\d+/g,'');
You can use replace function
window.location.href.replace('-1024x682', '');
If you want to use the new one as your current url then try
window.location.href = window.location.href.replace('-1024x682', '');

JAVASCRIPT/Chrome: How to Get Extension of Page

Given
google.com
How can I get .com and save it to a variable. I was thinking of using regex to split google.com into google and ".com", but I don't know the regex to do this.
It might help to know that I got the hostname from using window.location.hostname
Thank you!
You can split the location by ., and the last value in the array will be the extension. Something like this would work:
var extension=location.hostname.split(".");
extension=extension[extension.length-1];
console.log(extension)
JS Fiddle Example: https://jsfiddle.net/igor_9000/yvy0zrat/
Hope that helps!
You could use a substring and lastIndexof:
var str = window.location.hostname;
var ending = str.substring(str.lastIndexOf(".")+1);
https://jsfiddle.net/06r86q0n/
Mostly the same as the other answer, but neither helps with double domains like ".co.uk"
If you are looking to create a regex statement that will only grab the .com then I would use something like:
/(.com)/ as your Regex statement
so your code would look like something like this:
var regexMatch = /(.com)/; as a regular expression literal
or
var regexMatch = new RegExp(".com"); as constructor function of the RegExp object.
Do not use split, its a bit slower than slice:
var last = location.hostname
var t = last.slice(last.lastIndexOf("."));
console.log(t) // .com, .net etc...
Fiddle: https://jsfiddle.net/1qu0rffb/

Removing last part of URL based on

I need to remove any occurence of a product number that may occur in URLs, using javascript/jquery.
URL looks like this:
http://www.mysite.com/section1/section2/section3/section4/01-012-15_1571884
The final part of the url is always formatted with 2 digits followed by -, so I was thinking a regex might do the job? I need everything removing after the last /.
It must also work when the product occurs higher or lower in the hierarchy, i.e.: http://www.mysite.com/section1/section2/01-012-15_1571884
So far I have tried different solutions with location.pathname and splits, but I am stuck on how to handle differences in product hierarchy and handling the arrays.
DEMO
var x = "http://www.mysite.com/section1/section2/section3/section4/01-012-15_1571884";
console.log(x.substr(0,x.lastIndexOf('/')));
Use lastIndexOf to find the last occurence of "/" and then remove the rest of the path using substring.
var url = 'http://www.mysite.com/section1/section2/section3/section4/01-012-15_1571884';
parts = url.split('/');
parts.pop();
url = parts.join('/');
http://jsfiddle.net/YXe6L/
var a = 'http://www.mysite.com/section1/section2/01-012-15_1571884',
result = a.replace(a.match(/(\d{1,2}-\d{1,3}-\d{1,2}_\d+)[^\d]*/g), '');
JSFiddle: http://jsfiddle.net/2TVBk/2/
This is a very nice online regex tester to test your regexes with: http://regexpal.com/
Here is an approach that will properly handle a situation where there is no product ID as you requested. http://jsfiddle.net/84GVe/
var url1 = "http://www.mysite.com/section1/section2/section3/section4/01-012-15_1571884";
var url2 = "http://www.mysite.com/section1/section2/section3/section4";
function removeID(url) {
//look for a / followed by _, - or 0-9 characters,
//and use $ to ensure it is the end of the string
var reg = /\/[-\d_]+$/;
if(reg.test(url))
{
url = url.substr(0,url.lastIndexOf('/'));
}
return url;
}
console.log( removeID(url1) );
console.log( removeID(url2) );

How do I validate a URL path with a wildcard in Javascript?

str = 'http://*.foo.com/bar/' is a valid string.
How do I write a regex to validate this in JavaScript?
`http://xyz.foo.com/bar/` ✓ valid
`http://xyz.foo.com/bar/abc/` ✗ invalid
`http://xyz.foo.com/` ✗ invalid
try playing around at RegExr. It has a lot of good information and will give you a javascript regex at the bottom of the page when you are done.
Try this:
var url = // your url
url.match(/http://[a-zA-Z_0-9]+\.foo\.com/bar/$/g)
The $ matches the end of a string. The $ at the end will make sure that there is no text after it.
Can you give some more details?
e,g, /^http://[a-zA-Z]+.foo.com/bar/$/g
will do what you're looking for if it is on a single line (through ^$ delimiters). It will match xyz but not xyz1 which is easy to fix if you want to include numbers.
Play around it and let me know if you have more questions.
You can assign some of the location object's properties to your own variables once the page loads (since location is free to change afterward):
var hostURL = location.host; //should be '*.foo.com'
var pathURL = location.pathname; //should be '/bar'
Then create a RegExp object:
var regex = '.*\.foo\.com/bar/$';
var testURL = new RegExp(regex);
And test the URL:
if (testURL(hostURL + pathURL)) {
//do something
}
This regex oughta do it for you.
[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*#(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+(?:[A-Z]{2}|com|org|net|edu|gov)\b
Assign the above to a variable. And test if this pattern matches with the url of your choice using test() method of javascript. Update this to suit your needs if need be.

How to extract UUID from a uri using Regular Expression in Javascript?

I have this url string
http://apistaging.yoolk.com/listings/F7B519E8-135C-43D0-A35F-764B582EDC48?domain_name=cambodiastaging.yoolk.com&display=basic
I would like to get only the uuid F7B519E8-135C-43D0-A35F-764B582EDC48 from this url by using regular expression in Javascript. How could I do so? Please give me some suggestions.
Thanks you all.
/((\w{4,12}-?)){5}/.exec(URL)[0]
var url = 'http://apistaging.yoolk.com/listings/F7B519E8-135C-43D0-A35F-764B582EDC48?domain_name=cambodiastaging.yoolk.com&display=basi',
matches = url.match(/listings\/([A-F\d-]+)\?/),
UUID = matches[1];
console.log(UUID); // F7B519E8-135C-43D0-A35F-764B582EDC48
jsFiddle.
I would do it with splits.
var uri = 'http://apistaging.yoolk.com/listings/F7B519E8-135C-43D0-A35F-764B582EDC48?domain_name=cambodiastaging.yoolk.com&display=basic'
uri.split('?')[0].split('/')[4]

Categories