Javascript quotes [duplicate] - javascript

This question already has answers here:
Nesting quotes in JavaScript/HTML
(4 answers)
Closed 8 years ago.
I have a piece of javascript that creates an HTML table, saving each row of the table into an array. I want one of the table cells on each row to be clickable.
The following code works fine and when cell3 is clicked, it displays a message box:
array.push("<tr><td>cell1</td><td>cell2</td><td onclick='alert(this);'>cell3</td></tr>")
My problem is that if I wanted the message box to say "hello", I can't get it to work because of the quotes, any ideas?
It's a quote within a quote within a quote and my brain is melting.
Thanks for any help in advance.

try putting a backslash \ before the quotes around the word hello (for example).
you can programmatically do that if it works out for you when hardcoded... but let's see if it helps you first.
the following answer also suggests how to programmatically replace all quotes in your string with escaped quotes, so you don't have to hard code them:
How do I replace a double-quote with an escape-char double-quote in a string using javascript?
sample:
str.replace(/"/g, '\\"');

You can escape the " char using the \ before, for sample:
array.push("<tr><td>cell1</td><td>cell2</td><td onclick=\"alert(this);\">cell3</td></tr>");

Does this work?
var alertMessage = "HELLO!";
array.push("<tr><td>cell1</td><td>cell2</td>
<td onclick='alert(" + alertMessage +");'>cell3</td></tr>")

Related

Regex to remove a whitespace from href link [duplicate]

This question already has answers here:
Remove ALL white spaces from text
(14 answers)
Closed 6 years ago.
Can some one help me to create a regular expression in Javascript to remove a whitespace from href link and replace whitespace to hyphen to in my content?
For example:
<a class="card" href=http://www.eee.com/sffsd/sdfs/Aks's Reb outsider/4234234234324>
it should convert it into
<a class="card" href=http://www.eee.com/sffsd/sdfs/Aks's-Reb-outsider/4234234234324>
A couple of things, like I said in the comment, replacing the spaces with dashes should be as easy as:
link.href = link.href.replace(/ /g, '-');
//or in php:
$href = preg_replace('/ /', '-', $href);
(JS only): using the g flag ensures the entire string is searched for spaces, and replaces them all with dashes.
The better question to ask is: how did the spaces get there in the first place?
My first port of call would be to look at the code generating the markup, and fix the problem there. You shouldn't be writing code fixing the output of code that is broken. Fix the bug, don't accommodate it.
Arguably, the URL's should be properly escaped, rather tan using regex, the URI should've been passed through a function like encodeURI to convert all spaces to %20 etc...
use this regex format
link.href = link.href.replace(/\s/g, '-');

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.

javascript: How can I have a string that has code in it that I don't want to be executed? [duplicate]

This question already has answers here:
Are double and single quotes interchangeable in JavaScript?
(23 answers)
Closed 8 years ago.
I am building a website that teaches people how to code a website. I am trying to add a feature where they code the exercise into a text box and I then compare that to a string to see if they got it right or not yet. I am running into an issue though when there is quotes inside the strings answer because that then ends the string thus cutting off some of the answer. How can I get around this?
All feedback is greatly appreciated!
Here is an example of a strings answer that screws it up:
var answer = "var greeting="Hello World!"; ";
The second pair of quotes end the string's declaration early. Is there a way to include all of it including the second pair of quotes in the declaration?
You can:
Escape the quotes with \:
var answer = "var greeting=\"Hello World!\"; ";
var answer = 'var greeting=\'Hello World!\'; ';
Use different quotes:
var answer = "var greeting='Hello World!'; ";
var answer = 'var greeting="Hello World!"; ';

replace asterisk innerhtml javascript [duplicate]

This question already has answers here:
How do I replace an asterisk in Javascript using replace()?
(6 answers)
Closed 8 years ago.
how do i replace the asterisks with a blank ("") in the innerhtml using javascript?
i have tried this.
document.getElementById("lab").innerHTML = document.getElementById("lab").innerHTML.replace(/*/g, '');
and i also tried doing the asterisk itself but it gets commented..
searched everywhere i can't seem to find ways to remove the appended in innerhtml aside from replacing it with a blank.. are there any other options?
i also tried searching for ways to use replace and other options. for added info i am using this for the validation of a form
thanks in advance!
You have to escape the asterisk:
.....replace(/\*/g, "")
because it is a special character in regular expressions.

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