- Cut string url in javascript
- 5 Answers 5
- Get the URL without any parameters in JavaScript | Example code
- Get the URL without any parameters in JavaScript
- window location origin + pathname
- Rid of the query parameters using split method
- Javascript split url on filters
- 4 Answers 4
- JavaScript split and add a string
- 6 Answers 6
Cut string url in javascript
I have no idea what -65(www.anyweb.com) means in JavaScript? Is this an example of what you’re trying to do?
I’m pulling this whole string from backend. What I need to do was to take out only the url and assign as hyperlink to value -65(for example)
5 Answers 5
You need to extract the string after ‘(‘ and before ‘)’
var str = "-65(www.anyweb.com)"; str = str.substring(str.lastIndexOf("(")+1,str.lastIndexOf(")"));
You can use this example for string operations var data = "-65(www.anyweb.com)"; var url = data.slice(data.indexOf('(')+1 ,data.indexOf(')')); console.log("URL :: ",url);
var domain = /\((.*?)\)/.exec("-65(www.anyweb.com)")[1]; console.log(domain);
The regex above will create a group with anything that’s inside parenthesis.
You can use some simple string operations:
var str = "-65(www.anyweb.com)"; var url = "N/A"; // Find indices of open and close parentheses var open = str.indexOf("("); var close = str.lastIndexOf(")"); // If they were found then extract the URL from the string if (open !== -1 && close !== -1) < url = str.substring(open + 1, close); >console.log(url);
If you are more inclined to use regular expressions then this should do it:
var str = "-65(www.anyweb.com)"; var regex = /\((.*?)\)/; // Capture URL inside parentheses var result = regex.exec(str); // Execute the regex against the string var url = "N/A"; // If the URL was matched then assign it to the variable if (result[1] !== undefined) < url = result[1]; >console.log(url);
You can also simply replace the stuff that you do not want:
var str = "-65(www.anyweb.com)"; str = str.replace(/^.*\(/, ""); // Remove everything before URL str = str.replace(/\).*$/, ""); // Remove everything after URL console.log(str);
Get the URL without any parameters in JavaScript | Example code
There are multiple ways to Get the URL without any parameters in JavaScript.
First get the get current URL get rid of the query parameters.
const url = window.location.href.split('?')[0]
Second concat origin and pathname, if there’s present a port such as example.com:80, that will be included as well.
const url = window.location.origin + window.location.pathname //http://example.com/somedir/somefile/
window.location.origin will give you the base url, in our test case: http://example.com
window.location.pathname will give you the route path (after the base URL), in our test case /somedir/somefile
Get the URL without any parameters in JavaScript
window location origin + pathname
Using hardcoded URLs for example.
Rid of the query parameters using split method
Output: https://www.eyehunts.com/path/
Do comment if you have any doubts and suggestion on this JS URL tutorial.
Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.
OS: Windows 10
Code: HTML 5 Version
Javascript split url on filters
Im giving my users the possibility to filter products without refreshing the page with ajax. i update the url to make it look like : http://mywebsite.com/products/filter?style=7-1-2&price=4-5-7&brand=48-12-5&color=8-4 where the int values are id’s split by -. so i have the options: style price brand color what i want is get these values in a var for each filter option so that i end with :
var styleValues = 7,1,2 var priceValues = 4,5,7
if only price filter is selected the url will look like http://mywebsite.com/products/filter?price=4-5-7 so i cant split on the tags for the filters. I really like to know what would be the best way to turn the url to different vars. What i already know : how to get the filter part:
var filterPart =window.location.search;
4 Answers 4
Great article on css tricks covering just this: http://css-tricks.com/snippets/javascript/get-url-and-url-parts-in-javascript/ JavaScript can access the current URL in parts. For this URL:
window.location.protocol = «http:» window.location.host = «css-tricks.com» window.location.pathname = «example/index.html» So to get the full URL path in JavaScript:
var newURL = window.location.protocol + "//" + window.location.host + "/" + window.location.pathname;
If you need to breath up the pathname, for example a URL like http://css-tricks.com/blah/blah/blah/index.html, you can split the string on «/» characters
var pathArray = window.location.pathname.split( '/' );
Then access the different parts by the parts of the array, like
var secondLevelLocation = pathArray[0];
To put that pathname back together, you can stitch together the array and put the «/»‘s back in:
var newPathname = ""; for (i = 0; i < pathArray.length; i++) < newPathname += "/"; newPathname += pathArray[i]; >
function getQueryVariable(variable) < var query = window.location.search.substring(1); var vars = query.split("&"); for (var i=0;i )" data-controller="se-share-sheet" data-se-share-sheet-title="Share a link to this answer" data-se-share-sheet-subtitle="" data-se-share-sheet-post-type="answer" data-se-share-sheet-social="facebook twitter devto" data-se-share-sheet-location="2" data-se-share-sheet-license-url="https%3a%2f%2fcreativecommons.org%2flicenses%2fby-sa%2f3.0%2f" data-se-share-sheet-license-name="CC BY-SA 3.0" data-s-popover-placement="bottom-start">Share )" title="">Improve this answer answered Sep 28, 2014 at 15:52 vimes1984vimes1984 1,684 3 gold badges 26 silver badges 59 bronze badges