I have found that the following thread provides an extremely useful way to create permalinks or to pass string values via a URL:
Original Thread
Unfortunately, if you wanted to pass the string "test string", for example, to a specific <div> via the URL and display it as simple text, the above thread doesn't seem to decode white space if your URL looks like this:
http://www.abc123.org/subpage.html?test%20string
The code will simply take anything in the URL passed the "?" and it will appear as "simple%20text".
Is there a simple way to do something similar to the Thread's accepted answer so that all %20 can be replaced with white space? Thanks!
You can use decodeURI():
Replaces each escape sequence in the encoded URI with the character
that it represents, but does not decode escape sequences that could
not have been introduced by encodeURI. The character “#” is not
decoded from escape sequences.
const result = decodeURI('http://www.abc123.org/subpage.html?test%20string');
console.log(result);
Related
I need to read dot ner reesources string in java script as mention below.
var resources = #Html.ResourceStrings("Home_General_", Resources.ResourceManager);
The above line will render all the resources (from Dot net resource file) which start with resource key as "Home_General_"
Some of the values from the resources are like "Hi "XYZ" are you there" i.e The string contains quotes character.
If the string has quotes the above call fails.
The one way to avoid this problem is escape the special character as "Hi \"XYZ\" are you there"
Any other way where we can avoid this, As I don't want to pollute my resource string with lot of escape (\) characters.
You need to Javascript-escape any string when you render it as a Javascript string literal.
You must also remove the outer quotes from the string resource; that should be text, not a half-valid Javascript expression.
Use code like this to retrieve a single resource string:
var resourceXYZ = '#Html.Raw(HttpUtility.JavaScriptStringEncode(Resources.ResourceManager.GetString("Home_General_XYZ")))';
We do the following:
We get the resource string via Resources.ResourceManager.GetString().
We pass the result to HttpUtility.JavaScriptStringEncode to escape any special characters in JavaScript.
We pass the result to Html.Raw() to prevent Razor from applying HTML encoding on this string.
We then output the text enclosed in single quote quaracters into the page.
The function Html.ResourceStrings is not a standard function that is part of MVC. Someone at your place must have written it. If you show us this code, we could tell you how to rewrite it to return valid JavaScript literals.
You could wrap your #Html.ResourcesString(...) with HttpUtility.JavaScriptStringEncode which will handle all escape issues.
var resources = #HttpUtility.JavaScriptStringEncode(Html.ResourceStrings("Home_General_", Resources.ResourceManager));
This is my url with params look like:
http://localhost:8000/search?city=uk&cat=Sightseeing%20&%20Tours
But when I try to capture the cat params using:
var test2 = $location.search().cat;
it only return Sightseeing. How can I resolve this issues? Thanks!!
The ampersand (&) is the delimiter for URL parameters. This means that the cat parameter ends after Sightseeing%20. (The %20 is probably automatically converted to a space, rendering it invisible.)
There is a simple solution though; just percent-encode the ampersand; use %26. Note most programming languages provide some function to automatically percent-encode strings, in PHP for example it would be urlencode().
For more information, see https://en.wikipedia.org/wiki/Percent-encoding. It talks about reserved and unreserved characters in URLs, which is exactly what's causing your problem; the ampersand is a reserved character.
I'm working to integrate some code with a third party, and sometimes a string argument they pass to a Javascript function I'm writing will be encoded using encodeURIComponent, sometimes it won't be.
Is there a definitive way to check whether it's been encoded using encodeURIComponent
If not, I'll do the encoding then
You could decode it and see if the string is still the same
decodeURIComponent(string) === string
Not reliably, especially in the case where a string may be encoded twice:
encodeURIComponent('http://stackoverflow.com/')
// yields 'http%3A%2F%2Fstackoverflow.com%2F'
encodeURIComponent(encodeURIComponent('http://stackoverflow.com/'))
// yields 'http%253A%252F%252Fstackoverflow.com%252F'
In essence, if you were to try and detect the string encoding when the passed argument is not actually encoded but has qualities of an encoded string, you'd be decoding something you shouldn't.
I'd recommend adding a second parameter in the definition "isURIComponent".
However, if you wanted to attempt, perhaps the following would do the trick:
if ( str.match(/[_\.!~*'()-]/) && str.match(/%[0-9a-f]{2}/i) ) {
// probably encoded with encodeURIComponent
}
This tests that the non alphanumeric characters that don't get encoded are intact, and that hexadecimals exist (e.g. %20 for a space)
(edited) I am reading a JSON file that includes some UTF-8 characters that are encoded like this: "\uf36b". I am trying to write a RegExp to convert this to an HTML entity that looks like "🍫". This displays the character correctly in my html page.
I haven't been able to correctly display the character that should be associated with "\uf36b", especially when in a longer sentence that also includes other text.
How can I write a regexp that replaces strings like "\uf4d6" and "\uf36b" but leaves other text alone?
Example:
var str = "I need \uf36b #chocolate";
This should be converted to:
I need 🍫 #chocolate;
The \uf36b here is a Unicode code point that represents a character, it should be possible to have your page support characters like this without needing to escape them by encoding to UTF-8.
That being said, the printable ASCII range is from \u0020 (space character) to \u007e (tilde), so you should be able to use something like the following to only escape the characters you need to:
var escaped = "I need \uf36b #chocolate".replace(/[^\x20-\x7e]+|%/g, escape);
This will call escape() only on the non-ASCII or non-printable ASCII characters in your string, as well as any % characters.
You can then use var str = escaped.replace(/%(..)/g,"") + ";"; to do your conversion, although this looks pretty strange and I can't really see how it would do anything too useful. You probably actually want something like the following:
var str = escaped.replace(/%(?:u([0-9a-f]{4})|([0-9a-f]{2}))/gi, "&#x$1$2;");
I have the following code that is used to turn http URLs in text into anchor tags. It's looking for anything starting with http, surrounded by white space (or the beginning/end of input)
function linkify (str) {
var regex = /(^|\s)(https?:\/\/\S+)($|\s)/ig;
return str.replace(regex,'$1$2$3')
}
// This works
linkify("Go to http://www.google.com and http://yahoo.com");
// This doesn't, yahoo.com doesn't become a link
linkify("Go to http://www.google.com http://yahoo.com");
The case where it doesn't work is if I only have a single space between two links. I'm assuming it's because the space in between the two links can't be used to match both URLs, after the first match, the space after the URL has already been consumed.
To play with: http://jsfiddle.net/NgMw8/
Can somebody suggest a regex way of doing this? I could scan the string myself, looking for a regex way of doing it (or some way that doesn't require scanning the string my self and building a new string on my own.
Don't capture the final \s. This way, the second url will match the preceding \s, as required:
function linkify (str) {
var regex = /(^|\s)(https?:\/\/\S+)/ig;
return str.replace(regex,'$1$2')
}
http://jsfiddle.net/NgMw8/3/
Just use a positive lookahead when matching your final $|\s, like so:
var regex = /(^|\s)(https?:\/\/\S+)(?=($|\s))/ig;
None will work if there are any html element stuck to the url ...
Similar question and it's answers HERE
Some solutions can handle url like "test.com/anothertest?get=letsgo" and append http://
Workaround may be done to handle https and miscellaneous tld ...