Conver ASCII to plain text in Javascript? [duplicate] - javascript

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);

Related

How do I insert new line characters to a string when entered in an input box [duplicate]

This question already has answers here:
HTML newline using fromCharCode is not working
(3 answers)
html label inserting newline
(3 answers)
Closed 4 years ago.
I'm trying to replace substrings. The substitution is entered in an input box. The thing is that I am unable to insert \n via the input box.
So this does not work
var val = document.getElementById('val').innerHTML;
var replacement = document.getElementById('replaceWith').value
val.innerHTML = val.innerHTML.replace(reg, replacement + '$&');
But this does work
var val = document.getElementById('val').innerHTML;
var replacement = document.getElementById('replaceWith').value
val.innerHTML = val.innerHTML.replace(reg, "\n" + '$&');
Is there a way to accomplish this?

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

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

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);

Remove Substring in Javascript [duplicate]

This question already has answers here:
How to remove text from a string?
(16 answers)
Closed 7 years ago.
In JavaScript, I need to find a substring between &J= and Key using regex and remove it from my url which could contain several substrings.
Here is my url:
SID=18608202881669&Act=432&Mode=1&CLk=T&Key58=6003&dotnetdll=TopCoConfigurator.dll&dotnetfunc=CasingSizeSummary&SID=18608202881669&F=&J=CasingSize/CasingSizeSummary.asp&Key58=6003&ccs_casingsizeid=6003
Thanks
You can learn Regular Expression here and code for your problem is:
Here you go:
SID="18608202881669&Act=432&Mode=1&CLk=T&Key58=6003&dotnetdll=TopCoConfigurator.dll&dotnetfunc=CasingSizeSummary&SID=18608202881669&F=&J=CasingSize/CasingSizeSummary.asp&Key58=6003&ccs_casingsizeid=6003";
var re = /&J=(.*?)&Key/;
SID = SID.replace(re, '');
alert(SID);
See Fiddle

Regex struct in Javascript [duplicate]

This question already has answers here:
How to convert unicode in JavaScript?
(4 answers)
Closed 9 years ago.
I am getting data returned in a JSON file with Unicode characters. How to replace Unicode characters as per my example?
\u003cli class=\"channels-content-item\"\u003e\n
\n
\u003cdiv class=\"shmoovie-content-cell\"\u003e\n
\u003ca href=\"\/movie\/the-makeover\" class=\"ux-thumb-wrap contains-addto yt-uix-sessionlink\" data-sessionlink=\"ei=Oo21UdLqM8aDhgHc_IHYCA\"\u003e
After replacing with regex:
\u003c must be replaced by <
\u003e must be replaced by >
\/ must be replaced by /
\" must be replaced by "
How to do that?
Using the bit of the string you posted I put this fiddle together that shows how to just use the string value you have (like in this SO answer I mentioned in the comments).
HTML
<div id="content"></div>
JS
var s = "\u003cli class=\"channels-content-item\"\u003e\n\n\u003cdiv class=\"shmoovie-content-cell\"\u003e\n\u003ca href=\"\/movie\/the-makeover\" class=\"ux-thumb-wrap contains-addto yt-uix-sessionlink\" data-sessionlink=\"ei=Oo21UdLqM8aDhgHc_IHYCA\"\u003e";
var div = document.getElementById('content');
div.innerHTML = s;
console.log(s);
Which sets the HTML content for the div with the elements:
<li class="channels-content-item">
<div class="shmoovie-content-cell">
<a href="/movie/the-makeover" class="ux-thumb-wrap contains-addto yt-uix-sessionlink" data-sessionlink="ei=Oo21UdLqM8aDhgHc_IHYCA">
Although it's not valid HTML, javascript seems to figure it out, at least it does in Chrome.
Not sure that regex is the best idea...The below solves the problem using the replace function built into JavaScript.
DEMO: http://jsfiddle.net/abc123/5DFUb/1/
var str = "\u003cli class=\"channels-content-item\"\u003e\n \n \u003cdiv class=\"shmoovie-content-cell\"\u003e\n \u003ca href=\"/movie/the-makeover\" class=\"ux-thumb-wrap contains-addto yt-uix-sessionlink\" data-sessionlink=\"ei=Oo21UdLqM8aDhgHc_IHYCA\"\u003e";
var newStr = str.replace("\\u003c", "<").replace("\\u003e",">").replace("\\/","/").replace("\\\"","\"");
alert(newStr);

Categories