How can I do string interpolation? [duplicate] - html

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}.`;

Related

How to use regex (regular expressions) in Notepad++ to remove all HTML and JSON code that does not contain a specific string?

Using regular expressions (in Notepad++), I want to find all JSON sections that contain the string foo. Note that the JSON just happens to be embedded within a limited set of HTML source code which is loaded into Notepad++.
I've written the following regex to accomplish this task:
({[^}]*foo[^}]*})
This works as expected in all the input that is possible.
I want to improve my workflow, so instead of just finding all such JSON sections, I want to write a regex to remove all the HTML & JSON that does not match this expression. The result will be only JSON sections that contain foo.
I tried using the Notepad++ regex Replace functionality with this find expression:
(?:({[^}]*?foo[^}]*?})|.)+
and this replace expression:
$1\n\n$2\n\n$3\n\n$4\n\n$5\n\n$6\n\n$7\n\n$8\n\n$9\n\n
This successfully works for the last occurrence of foo within the JSON, but does not find the rest of the occurrences.
How can I improve my code to find all the occurrences?
Here is a simplified minimal example of input and desired output. I hope I haven't simplified it too much for it to be useful:
Simplified input:
<!DOCTYPE html>
<html>
<div dat="{example foo1}"> </div>
<div dat="{example bar}"> </div>
<div dat="{example foo2}"> </div>
</html>
Desired output:
{example foo1}
{example foo2}
You can use
{[^}]*foo[^}]*}|((?s:.))
Replace with (?1:$0\n). Details:
{[^}]*foo[^}]*} - {, zero or more chars other than }, foo, zero or more chars other than } and then a }
| - or
((?s:.)) - Capturing group 1: any one char ((?s:...) is an inline modifier group where . matches all chars including line break chars, same as if you enabled . matches newline option).
The (?1:$0\n) replacement pattern replaces with an empty string if Group 1 was matched, else the replacement is the match text + a newline.
See the demo and search and replace dialog settings:
Updates
The comment section was full tried to suggest a code here,
Let me know if this is a bit close to your intended result,
Find: ({.+?[\n]*foo[ \d]*})|.*?
Replace all: $1
Also added Toto's example

Specifically display the non-printable characters [duplicate]

This question already has answers here:
h:outputText does not break \r\n characters into new lines
(3 answers)
Closed 6 years ago.
I am using primefaces and I would like to display to users the non-printable characters ( \n,\t,\r - should be shown as they are, not rendered).
I use the <h:outputText> command. I read the content from a file.
For example, the file has the following content:
test
new line
a new line
In java, windows this renders as: test\n new line\n a new line.
The output should be the same :Hello\n newline\n a new line.
How can I do that? The values are not printed at all:
"Hello new line a new line".
Found solution:
To render properly (e.g '\n' should be displayed) I added a new backslash: \n became \\n.
Thank you,
Luisa
You can use the following method from the Apache Commons project on the desired strings in your backing bean (it escapes all Java-like special characters):
https://commons.apache.org/proper/commons-lang/javadocs/api-3.5/org/apache/commons/lang3/StringEscapeUtils.html#escapeJava-java.lang.String-
Example:
private String escapeString = StringEscapeUtils.escapeJava("Hello \\n this is new line");
public String getEscapeString() {
return escapeString;
}
with
<h:outputText value="#{testViewBean.escapeString}"/>
prints
Hello \\n this is new line
You need to set escape property false as by default it is true.
As explained by MkYong.
https://www.mkyong.com/jsf2/jsf-2-outputtext-example/

Extract string from HTML tag [VB.Net] [duplicate]

This question already has answers here:
How do I match any character across multiple lines in a regular expression?
(26 answers)
Closed 4 years ago.
How do I match and replace text using regular expressions in multiline mode?
I know the RegexOptions.Multiline option, but what is the best way to specify match all with the new line characters in C#?
Input:
<tag name="abc">this
is
a
text</tag>
Output:
[tag name="abc"]this
is
a
test
[/tag]
Aahh, I found the actual problem. '&' and ';' in Regex are matching text in a single line, while the same need to be escaped in the Regex to work in cases where there are new lines also.
If you mean there has to be a newline character for the expression to match, then \n will do that for you.
Otherwise, I think you might have misunderstood the Multiline/Singleline flags. If you want your expression to match across several lines, you actually want to use RegexOptions.Singleline. What it means is that it treats the entire input string as a single line, thus ignoring newlines. Is this what you're after...?
Example
Regex rx = new Regex("<tag name=\"(.*?)\">(.*?)</tag>", RegexOptions.Singleline);
String output = rx.Replace("Text <tag name=\"abc\">test\nwith\nnewline</tag> more text...", "[tag name=\"$1\"]$2[/tag]");
Here's a regex to match. It requires the RegexOptions.Singleline option, which makes the . match newlines.
<(\w+) name="([^"]*)">(.*?)</\1>
After this regex, the first group contains the tag, the second the tag name, and the third the content between the tags. So replacement string could look like this:
[$1 name="$2"]$3[/$1]
In C#, this looks like:
newString = Regex.Replace(oldString,
#"<(\w+) name=""([^""]*)"">(.*?)</\1>",
"[$1 name=\"$2\"]$3[/$1]",
RegexOptions.Singleline);

string interpreted incorrectly in angularjs

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/

Regular expression to remove HTML tags from a string [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Regular expression to remove HTML tags
Is there an expression which will get the value between two HTML tags?
Given this:
<td class="played">0</td>
I am looking for an expression which will return 0, stripping the <td> tags.
You should not attempt to parse HTML with regex. HTML is not a regular language, so any regex you come up with will likely fail on some esoteric edge case. Please refer to the seminal answer to this question for specifics. While mostly formatted as a joke, it makes a very good point.
The following examples are Java, but the regex will be similar -- if not identical -- for other languages.
String target = someString.replaceAll("<[^>]*>", "");
Assuming your non-html does not contain any < or > and that your input string is correctly structured.
If you know they're a specific tag -- for example you know the text contains only <td> tags, you could do something like this:
String target = someString.replaceAll("(?i)<td[^>]*>", "");
Edit:
Ωmega brought up a good point in a comment on another post that this would result in multiple results all being squished together if there were multiple tags.
For example, if the input string were <td>Something</td><td>Another Thing</td>, then the above would result in SomethingAnother Thing.
In a situation where multiple tags are expected, we could do something like:
String target = someString.replaceAll("(?i)<td[^>]*>", " ").replaceAll("\\s+", " ").trim();
This replaces the HTML with a single space, then collapses whitespace, and then trims any on the ends.
A trivial approach would be to replace
<[^>]*>
with nothing. But depending on how ill-structured your input is that may well fail.
You could do it with jsoup http://jsoup.org/
Whitelist whitelist = Whitelist.none();
String cleanStr = Jsoup.clean(yourText, whitelist);