Issue on URL using Javascript - javascript

I get this URL "R+C%20Seetransport%20Hamburg" passing the query string using Javascript , but i need to get the URL in this format "R%2bC+Seetransport+Hamburg" using C#
Code Used:
var listname = $(this).text();
var listname1 = listname.trim();
// var senderElement = e.target;
var afullUrl = '<%=SPContext.Current.Web.Url%>';
var aurl = afullUrl + "/_Layouts/15/RUM/View_Details.aspx?List_Name="+listname1;
window.location = aurl;

If you are looking for %20 to be replaced by + sign then you can do the following:
var listname1 = listname1.replace(/%20/g, "+");
The %20 represents a white space. And %2b represents +(plus sign).
The problem seems to lie in the Urldecoding. You can also try decodeURIComponent() for removing %20 back to whitespace.

Related

how to replace words in string using javascript?

i have a string,
mystr = 'public\uploads\file-1490095922739.jpg';
i want to replace
public\uploads
with " ", so that i just want to extract only file name ie
file-1490095922739.jpg
or like,
\uploads\file-1490095922739.jpg
how can i do this, is there any methods for this in js or can we do it by replace method.
i am performing the following steps,
var imagepath1;
var imagepath = 'public\uploads\file-1490095922739.jpg';
unwantedChar = 'public|uploads';
regExp = new RegExp(unwantedChar , 'gi');
imagepath = imagepath.replace(regExp , '');
imagepath1 = imagepath;
$scope.model.imagepath = imagepath1.replace(/\\/g, "");
please suggest me optimized method.
var input = "public\\uploads\\file-1490095922739.jpg";
var result = input.replace("public\\uploads\\", "");
This is what you're looking for, no need for fancy regexs :). More information about replace can be found here.
Maybe I don't understand the issue - but wouldn't this work?
var mystr = 'public\uploads\file-1490095922739.jpg';
var filename = mystr.replace('public\uploads', '');
If you want to get the part of the string after the last backslash character, you can use this:
var filename = mystr.substr(mystr.lastIndexOf('\\') + 1);
Also note that you need to escape the backslash characters in your test string:
var mystr = 'public\\uploads\\file-1490095922739.jpg';
What about just doing:
var imagepath = 'public\\uploads\\file-1490095922739.jpg';
$scope.model.imagepath = imagepath.replace('public\\uploads\\', '');
instead of using a bunch of unnecessary variables?
This way you're getting the file path, removing public\uploads\ and then setting the file path to $scope.model.imagepath
Note that this will only work if the image file path always matches 'public\uploads\*FILENAME*'.
var url = '/anysource/anypath/myfilename.gif';
var filename = url.slice(url.lastIndexOf('/')+1,url.length);
Search for the last forward slash, and slice the string (+1 because you don't want the slash), with the length of the string to get the filename. This way, you don't have to worry about the path is at all times.

Regex not working to remove string/whatever

How can I remove this string from href and update it ?
Example Url:
"localhost:21088/WcfDataService1.svc/Clients?$top=20&$select=Name,Products/Name,ID,People/FirstName,Products/Price,People/LastName&$expand=People"
What I am trying:
var stringToRemove = "Products" + "/";
var url = $("#qUrl").attr("href");
url = url.replace('/(' + stringToRemove + '\/\w+,)/g', '');
$("#qUrl").attr("href", url);
What I want:
"localhost:21088/WcfDataService1.svc/Clients?$top=20&$select=Name,ID,People/FirstName,People/LastName&$expand=People"
Update
Please don't hard code
If you are looking to remove all Products/..., than RegEx is /Products\/.*?,/g
Take a note that RegExp is written as is - without surrounding it with quotes.
var str = 'localhost:21088/WcfDataService1.svc/Clients?$top=20&$select=Name,Products/Name,ID,People/FirstName,Products/Price,People/LastName&$expand=People';
console.log(str.replace(/Products\/\w+,?/g, ''));
/**
* Replace with variable string
*/
var key = 'Products'; // Come from external source, not hardcoded.
var pattern = new RegExp(key+'/\\w+,?', 'g'); // Without start and end delimiters!
console.log(str.replace(pattern, ''));
var stringToRemove = "Products" + "/";
var url = $("#qUrl").attr("href");
url = url.replace(/Products\/Name,/g, '');
$("#qUrl").attr("href", url);
Modify the replace call , use regex without quotes

javascript for splitting url and removing last part

http://www.google.com/site!#656126.72367
In this url, how to split and remove the part from exclamatory mark when page loaded using JS.
I just want http://www.google.com/site
Use string replace method , match every character after ! with regular expression and replace with ""
var url = 'http://www.google.com/site!#656126.72367';
url = url.replace(/!.*/,"");
You could use:
var host = window.location.hostname; // will be www.google.com
var path = window.location.pathname; // will be /site
In the end, you will have:
var url = "http://" + host + path;
Note: you can also use window.location.protocol, which in this case is http::
var url = window.location.protocol + '//' + host + path;
Update: as suggested by Rajesh, the window.location object also has access to the hash:
var hash = window.location.hash; // will be 656126.72367
It might be useful to do a console.log(window.location) and see what's in there!
This method works even if the hash contains several ! or #
var url = 'http://www.google.com/site!#656126.72367';
url = url.substring(0, url.indexOf('!'));
document.write(url);
substring extracts the characters from a string, between two specified indices (in this case on the first occurence and then on !), and returns the new sub string.
jsFiddle demo
var url = "http://www.google.com/site!#656126.72367";
url = url.split('!')[0];
console.log(url);

How do i match text after a specific character?

I have a url that looks like this:
http://mysite/#/12345
How do I retrieve the text using regex after the /#/ which is essentially a token that I would like to use elsewhere in my javascript application?
Thanks.
You don't need regex here, just String#substr using String#indexOf:
var s = 'http://mysite/#/12345';
var p ='/#/'; // search needle
var r= s.substr(s.indexOf(p) + p.length);
//=> 12345
Let the browser do it for you
var parser = document.createElement('a');
parser.href = "http://mysite/#/12345";
alert(parser.hash.substring(2)); //This is just to remove the #/ at the start of the string
JSFiddle: http://jsfiddle.net/gibble/uvhqa4yv/
Try with JavaScript String methods.
var str='http://mysite/#/12345';
alert(str.substring(str.lastIndexOf("/#/")+3));
You can try with String'smatch() method as well that uses regex expression.
Just get the matched group from index 1 that is captured by enclosing inside the parenthesis (...)
var str='http://mysite/#/12345';
alert(str.match(/\/#\/(.*)$/)[1]);
Using the browser to parse the URL and getting the hash would probably be most reliable and would work with any valid URL
var url = 'http://mysite/#/12345';
var ele = document.createElement('a');
ele.href = url;
var result = ele.hash.slice(2);
FIDDLE
or you can just split and pop it
var result = url.split('#/').pop();

Javascript remove characters utill 3 slash /

Whats the best to way, based on the input below, to get everything in the url after the domain:
var url = "http://www.domain.com.uk/sadsad/asdsadsad/asdasdasda/?asda=ggy";
var url = "http://www.domain.com.uk/asdsadsad/asdasdasda/#45435";
var url = "http://www.domain.com.uk/asdasdasda/?324324";
var url = "http://www.domain.com.uk/asdasdasda/";
The output:
url = "/sadsad/asdsadsad/asdasdasda/?asda=ggy";
url = "/asdsadsad/asdasdasda/#45435";
url = "/asdasdasda/?324324";
UPDATE: the domain its not always the same. (sorry)
Thx
You should really parse the URI.
http://stevenlevithan.com/demo/parseuri/js/
Every absolute URL consists of a protocol, separated by two slashes, followed by a host, followed by a pathname. An implementation can look like:
// Search for the index of the first //, then search the next slash after it
var slashOffset = url.indexOf("/", url.indexOf("//") + 2);
url = url.substr(slashOffset);
If the domain is always the same, a simple replace will work fine:
var url = "http://www.domain.com.uk/sadsad/asdsadsad/asdasdasda/?asda=ggy";
var afterDomain = url.replace("^http://www.domain.com.uk/", "");
You could also use RegEx:
var url = "http://www.domain.com.uk/sadsad/asdsadsad/asdasdasda/?asda=ggy";
var afterDomain = url.replace(/^[^\/]*(?:\/[^\/]*){2}/, "");
Assuming this is in the browser, creating an anchor element will do a lot of magic on your behalf:
var a=document.createElement('a');
a.href="http://somedomain/iouhowe/ewouho/wiouhfe?jjj";
alert(a.pathname + a.search + a.hash); // /iouhowe/ewouho/wiouhfe?jjj

Categories

Resources