javascript - how to remove uri from a link - javascript

I got a link lie this
url = 'http//mysite.com/product/id/122?u=12'
I want to turn that to this
'http//mysite.com'
remove all the uri and query string
How you guys do it?

You can use URL api
let urlParsed = new URL("http://example.com/product/id/122?u=12")
let {origin} = urlParsed
console.log(origin)

Use split and string concatenation
var url = 'http//mysite.com/product/id/122?u=12'
console.log(url.split('.com/')[0]+'.com')

Related

Find and replace a substring after a word in javascript

I have a string which has email id as plain text in it ,I want to replace the email id in the string with hashed value eg.
var str ="Token=wUFvvW4pLDjO2Kh9BkF6ShNXMpWCAH84RQrF2GMSMvkT9ji1HWER/hPcDzQVZ+eqfBnzltOP0+NJTa/x6+XrKcSR090Jka8Awdj13CiSiD5OXwFbCHzYX0nzwkbWJ3m7zvyvjIWJJZ7L53YRHckAeTzA39UWR53/s8PHyL7hUu8=&ssoComplete=true&userId=testmail5#gmail.com|flca+&siteID=OXchfjbB"
Before storing the above string I want to hash only testmail5#gmail.com which is after userId=. Hence need suggestions to achieve the same.
Using split twice
var str="Token=wUFvvW4pLDjO2Kh9BkF6ShNXMpWCAH84RQrF2GMSMvkT9ji1HWER/hPcDzQVZ+eqfBnzltOP0+NJTa/x6+XrKcSR090Jka8Awdj13CiSiD5OXwFbCHzYX0nzwkbWJ3m7zvyvjIWJJZ7L53YRHckAeTzA39UWR53/s8PHyL7hUu8=&ssoComplete=true&userId=testmail5#gmail.com|flca+&siteID=OXchfjbB"
console.log(str.split('userId=')[1].split('|')[0])
Try this :
str.match(/\userId=(.*?)\|/)[1]
Regex are better to do stuff like that
Quick and dirty way by using URL API in JS.
var data = "Token=wUFvvW4pLDjO2Kh9BkF6ShNXMpWCAH84RQrF2GMSMvkT9ji1HWER/hPcDzQVZ+eqfBnzltOP0+NJTa/x6+XrKcSR090Jka8Awdj13CiSiD5OXwFbCHzYX0nzwkbWJ3m7zvyvjIWJJZ7L53YRHckAeTzA39UWR53/s8PHyL7hUu8=&ssoComplete=true&userId=testmail5#gmail.com|flca+&siteID=OXchfjbB";
var some_hash = "myHashValue";
var data_url = new URL("http://localhost/?"+data); // dirty part
var new_data = data.replace(data_url.searchParams.get("userId").split('|')[0], some_hash);
console.log(new_data);

How do I replace characters using Javascript in HTML file?

For example I have a string getting from current URL using javascript
hostname/report/searchDate?searchOrderID=&searchDateFrom=2018-10-16&searchDateTo=2018-10-23&search=search&sortBy=OrderDateAsc
How do I replace character in the end OrderDateAsc ? If I want to replace for example , OrderDateDesc , how should I do it using Javascript ? The URL infront might be differs all the time , they keyword should be &sortBy.
Please help ,thanks.
var currentUrl = window.location.href;
var url = new URL(currentUrl);
var c = url.searchParams.get("sortBy");
You can get sortBy value like this, and maybe you can use some if statements for if set or not , and you can set like this:
url.searchParams.set('sortBy',"WhateverYouWant");
Also,convert url to string, you can read parameters url.searchParams.get("sortBy"); and again some if statements,
if change :
url = url.Replace(sortByValue,"WhateverYouWant");
this will works too.
But if your string looks like that and you try to change search value, then you will change every "search" value and its not work. :
hostname/report/searchDate?searchOrderID=&searchDateFrom=2018-10-16&searchDateTo=2018-10-23&search=search&sortBy=OrderDateAsc
change search as WhateverYouWant
hostname/report/WhateverYouWantDate?WhateverYouWantOrderID=&WhateverYouWantDateFrom=2018-10-16&WhateverYouWantDateTo=2018-10-23&WhateverYouWant=WhateverYouWant&sortBy=OrderDateAsc
You see, its bad :)
You could do it with the following code.
//your string
let str = "hostname/report/searchDate?searchOrderID=&searchDateFrom=2018-10-16&searchDateTo=2018-10-23&search=search&sortBy=OrderDateAsc";
//find "OrderDateAsc" and replace it with "OrderDateDesc"
str = str.replace("OrderDateAsc", "OrderDateDesc" );
you can use regular expression and replace the sortBy param value like that:
const url ='hostname/report/searchDate?searchOrderID=&searchDateFrom=2018-10-16&searchDateTo=2018-10-23&search=search&sortBy=OrderDateAsc';
const regex = /(&sortBy=OrderDate)(Asc)/gi;
const newUrl = url.replace(regex, '$1Desc');

How to get the first string from the URL?

I'm a newbie in web development so pls forgive my newbie question.
I have a URL "https://123asd.my.website.com/blabla/blabla/blabla
What I'm trying to figure out is how do I get the "123asd" so that I can set in on my var. Thank you
You can use regex
var url = 'https://123asd.my.website.com/blabla/blabla/blabla';
var number = url.match(/([0-9a-z]{1,})\./)[1];
console.log(number);
const url = "https://123asd.my.website.com/blabla/blabla/blabla";
let firstStr = url.replace("https://", ""); // get rid of "https://" or you can do it by some other way
firstStr = firstStr.substring(0, firstStr.indexOf('.')); // get the substring start from the beginning to the first '.'
console.log(firstStr); // 123asd
var url="https://123asd.my.website.com/blabla/blabla/blabla";
var urlNoHttps=url.replace(/^https?\:\/\//i, "");
var hostName=urlNoHttps.split('.')[0];
console.log(hostName);
The above code works for both http and https protocol.

Get the url Parameter string from Javascript

I need the whole parameter list as such , not one by one
var Url = "http://localhost/Home/Admin?param1=1&param2=2$param3=3";
I want to get the whole parameter list from the url.
var params = "param1=1&param2=2&param3=3";
var Url = "http://localhost/Home/Admin?param1=1&param2=2$param3=3";
var urlArray = url.split("?");
var params=urlArray[1];
You can see Using split() example of Mozilla Developer Network for more insight on using the split function.
Thanks for the support, I use this one for my need
var params = window.location.href.split('?')[1];

How to find current page url (like "index.html")?

With string http://www.example.com/section_one/index.html how can I return index using RegExp?
P.S.: Returning index.html is accepted too.
You don't need to use regex for that. Its simple:
var url = document.URL;
var currentPage= url.split("/").pop();
You can try the following.
Fiddle
var url = "http://www.example.com/section_one/index.html";
var filename = url.match(/[^\\/]+$/)[0];

Categories