string interpreted incorrectly in angularjs - html

I have a string in golang as follows.
discount = "("+discount+"% off)"
when passed to html via angularjs it is displayed as follows
(10 %o(MISSING)ff)
Any idea why it is happening?
Thanks in advance.

Something in your HTML rendering process is passing the string through go's fmt.Sprintf or similar. Try escaping the % by doubling it:
discount = "("+discount+"%% off)"
See http://play.golang.org/p/S_GEJXSfnD for a live example.

Looks like you need to escape the string. Try to use this module: http://golang.org/pkg/html/

Related

Rendering VueJS prop within HTML array

I have an HTML array with data like so:
data-groups='["category_all", "data goes here"]'
I have a prop called "title" which contains the string I need to render in the "data goes here" area. I've tried using v-bind, but then I lose the array which I need to have in order for the original sort feature to work.
I google'd a few different ways to either escape or render quotes, and most refer to v-bind which again, won't work in this instance.
Any help would be appreciated :)
I was using Shuffle.js and for anyone else seeking an answer, it was in the documentation:
https://vestride.github.io/Shuffle/docs/getting-started
Alternatively, you can set the delimiter option to a comma (delimiter: ',') and the data-groups attribute will be split on that character.
Then changing the above line of code to:
:data-groups="item.category.title + ',all'"
works just fine :)

How can I do string interpolation? [duplicate]

This question already has answers here:
How can I do string interpolation in JavaScript?
(21 answers)
Closed 3 years ago.
I'm trying to do string interpolation in code using Angular, Typescript, HTML, etc... For some reason the 'dollar sign' appears as a string literal in the output. Can anyone help?
I need the output to as follows:
"Hello World."
Instead, I'm getting this:
"Hello ${name}."
Link:
https://stackblitz.com/edit/angular-template-string-interpolation
Thanks.
To interpolate strings, you need to use template literals
Template literals use ``` (backtick) and not ' (single quote)
Also, name is a class property, so you should reference it by using this
So, your code becomes:
title: string = `Hello ${this.name}.`;
String interpolation only happens inside back ticks.
"Hello ${name}."
produces Hello ${name}. but
`Hello ${name}.`
produces Hello previewFrame in your example. This is because you are referencing the wrong variable. Instead of ${name} use ${this.name} otherwise you're getting the name of the frame that stackblitz renders your output in.
For the record, this is not a typescript or Angular feature. String interpolation is a newer vanilla javascript feature. More info here.
You need to use the this keyword and the back ticks. Like this:
title: string = `Hello ${this.name}.`;

Are there any ready solutions for string(html) parsing with actionscript?

I need to parse string entered to textarea for 2 reasons:
Remove all html tags except small number that are allowed;
Check if html tags are positioned correctly: <i><b> wow </i></b> is incorrect.
Are there any 'build-in' solutions or libraries for that deal? Using regExp is not ok for this task - it's very complex and generally bad idea to parse html structure with them.
You can try the unescape function.
Usage : clean_String = unescape( input_String );
Or else...
You can try the decode URI component function.
Usage : clean_String = decodeURIComponent( input_String );
In each case clean_String is an empty string that will be updated by the result of the functions. input_String is the string with the HTML entities that is to be decoded or unescaped etc.

How to write a regular expression for URLs

I've been using the Regular Expression Explorer but I still can't come up with the right pattern.
Here's my URL:
http://pie.crust.com:18000/TEST/TEST.html
Here's my RegExp:
/[^http:\/\/][\w-\W]+[\/]/
And the output is:
ie.crust.com:18000/TEST/
All I want is the domain (basically everything inbetween // and /):
pie.crust.com:18000
What am I missing? I just can't figure it out. Any ideas?
Thank you in advance.
Try this one: http:\/\/([^\/]+)
The part [^http:\/\/] is the same as [^htp:\/] and just enumerates all the characters which shouldn't be in the start part of the resulting string. So for http://pie.crust.com:18000/TEST/TEST.html http://p matches this enumeration. I suggest you the following expression:
/http:\/\/([^\/]+)\/.*/
You can use String.replace() the following way:
var myUrl:String = "http://pie.crust.com:18000/TEST/TEST.html";
var refinedUrl:String = myUrl.replace(/http:\/\/([^\/]+)\/.*/, "$1");
Try this:
#http://+(.*?)/#
(Your regexp doesn't have to start and end with / - it's easier to use something else that isn't in your search string.
(?<=http:\/\/)[a-zA-Z.:0-9-]+
The p of "pie" is being matched as part of the http rule, and so is not included. Using a positive look-behind fixed this.
http://regexr.com?2uhjf
try this...
//http:\/\/([^\/]+)\/.***/

Encode HTML before POST

I have the following script, which encodes some of the value it receives propertly, but it does not seem to encode double quotes.
How do I encode the full value properly before posting?
function htmlEncode(value){
return $('<div/>').text(value).html();
}
The above script give me this:
<p>Test&nbsp; <span style="color: #ffffff"><strong><span style="background-color: #ff0000">1+1+1=3</span></strong></span></p>
I need it to give me this:
<p>Test&nbsp; <span style="color: #ffffff"><strong><span style="background-color: #ff0000">1+1+1=3</span></strong></span></p>
EDIT: Followup question:
Encoded HTML in database back to page
You shouldn't try to encode things with JavaScript.
You should encode it serverside.
Anything that can be done with JavaScript can be undone.
It is valid to encode it in JavaScript if you also check that it was encoded on the server, but keep in mind: JavaScript can be disabled.
What George says is true.
But, if you have to encode strings client-side, I'd suggest you use JavaScript's encodeURIComponent().
I had a similar problem. I simply used the replace method in javascript. Here's a nice article to read: http://www.w3schools.com/jsref/jsref_replace.asp
Basically what the replace method does is it swaps or replaces the character it founds with what you indicate as replacement character(s).
So this:
var str=' " That " ';
str = str.replace(/"/g,'"');
Once you log this into the console of your browser, you will get something like
" That "
And this:
var str=' " That " ';
str = str.replace(/"/g,'blahblahblah');
Once you log this into the console of your browser, you will get something like
blahblahblah That blahblahblah
You can use this module in js, without requiring jQuery:
htmlencode
You can re-use functions from php.js project - htmlentities and get_html_translation_table
Use escape(str) at client side
and
HttpUtility.UrlDecode(str, System.Text.Encoding.Default); at server side
it worked for me.