How to remove html encoding from a string in angular 4? [duplicate] - javascript

This question already has answers here:
angularjs to output plain text instead of html
(8 answers)
Closed 5 years ago.
I have a very simple requirement. I have two strings as follows :
htmlStr = "<strong>News Body</strong>";
htmlStr1 = "<strong>News Body2</strong>";
I use innerHTML of div tag to display these strings on a html page
<div [innerHTML]="htmlStr"></div>
<div [innerHTML]="htmlStr1"></div>
Output I get is
My expected output is :
How do i remove html encoding from htmlStr?

The output is correct. You should replace the special characters with the brackets by e.g.
strHtml = strHtml.split(‘<’).join(‘<‘).split(‘>’).join(‘>’)

Try pipe for dynamic encoding html string
stackblitz demo - it's working correct do like this

Related

Remove all HTML tags *AND* content from json response [duplicate]

This question already has answers here:
RegEx match open tags except XHTML self-contained tags
(35 answers)
Closed 5 years ago.
I am aware of this solution - str.replace(/<\/?[^>]+>/gi, '') - which removes the HTML tags from json response.
{"fdfd":4}<p>fdfdf</p> -> {"fdfd":4} fdfdf
However i want to remove all content within the HTML, script tags as well for which i am seeking a solution.
Requirement -
{"fdfd":4}<p>fdfdf</p> -> {"fdfd":4}
Use string match and keep only those characters which are inside {}
var str = '{"fdfd":4}<p>fdfdf</p>'
var m = str.match(/{(.*)}/g)[0]
console.log(m)
To do that you have to use this regex
var str='{"fdfd":4}<p>fdfdf</p>';
console.log(str.replace(/<[^>]+>[^\n]+<\/?[^>]+>/gi, ''));
Added runnable code:
var str = '{"fdfd":4}<p>fdfdf</p>';
str = str.replace(/<\/?.*[^>*]+>/gi, '');
console.log(str);

Conver ASCII to plain text in Javascript? [duplicate]

This question already has answers here:
HTML Entity Decode [duplicate]
(17 answers)
Closed 6 years ago.
I have some vars with sentences in them, where I have to replace the apostrophe with "'". However, I'd like for a user to be able to tweet these out. When I add them to the tweet, it obviously just writes the ASCII code out as if it were normal English. Is there a way to convert these into proper punctuation before they're sent off to be tweeted?
If you are using jQuery, you could do it like this:
$('<div>').html("'").text();
If you are using plain JavaScript, you could do it like this:
var element = document.createElement('div');
element.innerHTML = "'";
element.textContent;
Here are two runnable examples:
jQuery:
console.log($('<div>').html("'").text());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
plain JavaScript:
var element = document.createElement('div');
element.innerHTML = "'";
console.log(element.textContent);

Get text between tags regex javascript [duplicate]

This question already has answers here:
Regular expression to get a string between two strings in Javascript
(13 answers)
Closed 4 years ago.
I am trying to get the text between the two tags which also handles multiple lines.I have managed to do it in PHP regex but i am stuck on the javascript one.
<any_tag>random text
dffdffdfdfdfdfdfdfd
dfdfdfdfdfdfdfdfdf
</any_tag>
I want to get the text between <any_tag> and </any_tag>
This is the regex for php
https://regex101.com/r/tQ1bX7/2
Now i am trying to achieve to same in javascript
Give try on following :
/[^(<any_tag>)](?:.*(\r?\n?).*)*(?=\<\/any_tag\>)/gi
Explination :
[^(<any_tag>)] match a single character not present in the list below
(<any_tag>) a single character in the list (<any_tg>) literally (case insensitive)

Jquery getting a url id [duplicate]

This question already has answers here:
How to get URL parameter using jQuery or plain JavaScript?
(34 answers)
Closed 7 years ago.
I am trying to get an id from a url string generated in php depending on the page you are viewing the id can appear in different places for example
site.com/view.php?id=12&person=23
or
site.com/view.php?loc=man&id=1782&person=43
I just need to get the id section using JQuery any ideas
You can use regex to get the id from URL.
var id = url.match(/id=(\d+)/)[1]
DEMO
REGEX Explanation
/: Delimiters of regex
id=: Matches literal id=
() : Capturing group
\d+: Matches one or more numbers

How to add Unicode to HTML? [duplicate]

This question already has answers here:
How do I correctly insert unicode in an HTML title using JavaScript?
(2 answers)
Closed 7 years ago.
help me pls.
In Unicode symbol & = "&" + "#38;" If I add it to HTML like this
<div id="div" title="&"></div>
ALL OK! I create symbol in title, like this
div id="div" title="&"></div>
But, when I add Unicode in HTML via JavaScript:
var div = document.getElementById('div');
div.setAttribute('title', "&");
I have a bad result:
<div id="div" title="&"></div>
How I can add Unicode in html attribute via JavaScript and get the correct result like it:
<div id="div" title="&"></div>
Thanks for help!
AND
IF I do it:
var code = 26;
div.setAttribute('title', "\x" + code);
I have a error. How I can fix it?
HTML entity escaping (e.g. &) is, as the name implies, only necessary in HTML. It's not necessary in Javascript; you can set the character literally:
div.setAttribute("title", "&");
If you need to escape a character, you can do so using a hexadecimal character escape:
div.setAttribute("title", "\x26");
or a Unicode character escape:
div.setAttribute("title", "\u0026");
For the & character in Javascript, you do not have to escape it. Otherwise, you should can escape Unicode in Javascript with \u, but most of the time you will not have to.

Categories