Url split in javascript

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); 

Источник

Читайте также:  Javascript select div by style

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
Add a comment |
0

maybe this can help you :

var Request = < QueryString : function (item) < var svalue = location.search.match(new RegExp("[\?\&]" + item + "=([^\&]*)(\&?)","i")); return svalue?svalue[1]:svalue; >, queryAllString : function() < var urlLocation = location.href; var startPosition = urlLocation.indexOf("?"); if (startPosition < 0) < return ''; >else < return urlLocation.slice(startPosition); >> > 

If you want to get price,you can do like this:

Источник

JavaScript split and add a string

how to insert a string inside the url address after split ? I have a simple code like this, but I just don't understand how split and join are work I have tried "append" function but I can't get it right I test and write it in http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_split

     

Vikenoshi, please remember to accept the answer which has helped you :). Thanks and welcome to stackoverflow!

6 Answers 6

str.replace('image/', 'image/original/'); 

if you really want to convert it into an array for some reason:

var ary = str.split('/'); ary.splice(2, 0, 'original'); ary.join('/'); 

vikenoshi, You want to use the Array.splice method to insert new elements into your resulting array that you created using String.split . The splice method is documented here:

Here is the code which should do what you want:

I created a JsFiddle with a working example: http://jsfiddle.net/S2Axt/3/

A note about the other methods I'm using:

  • join : Join creates a new string from an array. This string is constructed by transforming all of the elements of the array into a string, and appending or concatenating them together. You can optionally provide a delimitter. Here I use the / to split the portions of the path.
  • split : Split splits a string based on another string into an array.
var wholeURL = "/image/picture.jpg"; var choppedUpURL = wholeURL.split("/"); var finalURL = "/" + choppedUpURL[1] + "/original/" + choppedUpURL[2]; alert(finalURL); 
var str="/image/picture.jpg"; var elems = str.split("/"); elems.splice(elems.length-1, 0, "original") document.write(elems.join("/"); 

Note I'm using the splice method with a first argument of the length of the array - 1. This puts the string "original" in the second to last position in the final path, not matter how long the URL you pass in. If this isn't the desired behavior, you can change the code to read elems.splice(2, 0, "original") . This would put the string "original" in the second position in the path, no matter how long the URL is.

Источник

Оцените статью