Decode url string in javascript give empty string - javascript

I have this string:
http:\/\/www.emptyurl.com\/
And i want to change the url to :
http://www.emptyurl.com/
With :
url = url.replace(/\\//gi, "/");
But it give me empty string.
Any idea how i can fix it?

You can use decodeURIComponent()
var url = 'http:\/\/www.emptyurl.com\/';
url = decodeURIComponent(url);
console.log(url);

Related

Remove (#) from URL in Silverlight

I have a URL e.g. http://localhost:8000/#Test/Method
I am doing following:
System.Windows.Browser.HtmlPage.Window.CurrentBookmark = string.Empty;
which just remove Test/Method but not '#'
I need to modify browser URL
as:
http://localhost:8000/
Any ideas on how to fix this?
If you are manipulating a String you can use the following method :
String Url = "Foo#Bar";
Url = Url.Replace("#", string.Empty);
Use regular expression
String url = "http://localhost:8000/#Test/Method"
url = url.replace(/#/g, "");
The above reg expression will scan for all occurrences of '#' character and replaces with empty character

Can not get content with special character URL in javascript?

I'm using javascript to get html content from url. I'm follow code in below :
function getContentHTML(theUrl) {
$.ajax({
url: theUrl,
crossDomain: true,
success: function (data) {
$("#divcontent").html(data);
}
});
}
If URL is in English(ex : bbc.com,voa.com....),It will be ok,but if url have contain special character, I can not get content.How can I slove?U can test in my URL.
URL : https://tw.news.yahoo.com/%E7%A9%BA%E9%9B%A3%E8%BB%8D%E6%96%B9%E5%A4%A7%E6%94%B9%E5%8F%A3-%E7%A2%BA%E5%AF%A6%E6%9C%89%E4%BB%8B%E5%85%A5%E8%88%AA%E7%AE%A1-120050818.html
Special characters are not allowed in the url. You need to encode the special characters. Use encodeURI function available in javascript. Check rfc 1738 for more details.
try this function to decode urls:
var uri_dec = decodeURIComponent(encodedUrl);
or
var uri_dec = decodeURI(encodedUrl);
This is because url is encoded,browser will automatically put some characters in place of special characters in url for example in place of space in url %20 gets inserted.
So you have to encode decode url,explained very clearly in there links :
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/decodeURIComponent
var encodeduri=https://tw.news.yahoo.com/%E7%A9%BA%E9%9B%A3%E8%BB%8D%E6%96%B9%E5%A4%A7%E6%94%B9%E5%8F%A3-%E7%A2%BA%E5%AF%A6%E6%9C%89%E4%BB%8B%E5%85%A5%E8%88%AA%E7%AE%A1-120050818.html
var uri_decoded = decodeURIComponent(encodeduri); //The decodeURIComponent() function decodes a URI component.
OR
var uri_decoded = decodeURI(encodeduri); //The decodeURI() function is used to decode a URI.

javascript regular expression replacement doesnt work?

why this regular expression replacement doesnt work?
var url = 'http://myweb.com/page/1/id/2';
newUrl = url.replace('/page\/[0-9]+/', 'page/2'); //it must become http://myweb.com/page/2/id/2
You need to do two things:
change str.replace to url.replace
remove the ' around the regex
var url = 'http://myweb.com/page/1/id/2';
newUrl = url.replace(/page\/[0-9]+/, 'page/2');
Example: http://jsfiddle.net/fhqXn/
Use url rather than str, if you want to replace something in the String stored in the url variable.
You have a naming misspell.
Rename your url var to str or change the str.replace to url.replace:
newUrl = url.replace('/page\/[0-9]+/', 'page/2');

How to convert signs in url/text to hex characters? (converting = to %3D)

With the script I'm making, jquery is getting vars from url parameter. The value that its getting is an url so if its something like
http://localhost/index.html?url=http://www.example.com/index.php?something=some
it reads:
url = http://www.example.com/index.php?something
If its like
http://localhost/index.html?url=http://www.example.com/index.php?something%3Dsome
it reads:
url = http://www.example.com/index.php?something%3Dsome
which would register as a valid url. my question is how can I search for = sign in the url variable and replace it with hex %3D with jquery or javascript?
Use the (built-in) encodeURIComponent() function:
url = 'http://localhost/index.html?url=' +
encodeURIComponent('http://www.example.com/index.php?something=some');
Are you looking for encodeURIComponent and decodeURIComponent?

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