How do i parse the following string
var a = JSON.parse('[' + '{"NoteName":"it's my life","UserId":"100","NoteActive":true,"UserEmail":"admin#dev.xrc.com","CreatedDate":"8/13/2012 1:47:35 PM"}' + ']');
You have just to escape a single quote it\'s
var a = JSON.parse('[' + '{"NoteName":"it\'s my life","UserId":"100","NoteActive":true,"UserEmail":"admin#dev.xrc.com","CreatedDate":"8/13/2012 1:47:35 PM"}' + ']');
console.log(a);
Replace it's with it\'s
'[' + '{"NoteName":"it\'s my life","UserId":"100","NoteActive":true,"UserEmail":"admin#dev.xrc.com","CreatedDate":"8/13/2012 1:47:35 PM"}' + ']'
You can escape (interpret solely as characters) quote marks using backslash.
"\"" or '\''
Related
I am trying to make a unicode code points table that prints the code points till U+300
I change the number into hexadecimal and concatenate it with the unicode escape sequence.
When I try to concatenate the hexadecimal number with '\u' I get an error SyntaxError: Invalid Unicode Escape Sequence
Here's the code
How can I fix that error?
Change the print statement to print(num + ' => ' + String.fromCharCode("0x" + num));
Instead of this:
print(num + ' => ' + '\u' + num);
use this:
print(num + ' => ' + '\\u' + num);
Or, more concisely,
print(num + ' => \\u' + num);
You need to escape the \ itself to include it in a string literal.
I have an HTML element like this
<div id='test' onclick='window.location="http://example.com/";'></div>
I'm annoyed because I would like to put this into a string but I'm not able since this element use both ' and " in its syntax.
How can I store such syntax into a string?
Try This:
var data = "<div id='test' onclick='window.location=\"http://example.com/\";'></div>"
alert(data)
Just escape the strings with a \.
var myString = "<div id='test' onclick='window.location=\"http://example.com/\";'></div>";
escape the ' or " with a backslash
var testString = '<div id=\'test\' onclick=\'window.location=\"http://example.com/\";\'></div>';
alert(testString);
you can use backslash \ for putting special character in string
for example for storing string "(double quotes) and '(single quotes) you can write
var s="\"\'"
var storeName = "St. Bob's Store";
var storeId = storeName.replace(/./g,"").replace(/\s/g, '').replace(/'/g,"")
$('#storeName').html(storeName)
$('#storeId').html("(" + storeId + ")")
console.log("Updating " + storeName + "(" + storeId + ")");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="storeName">Loading</div>
<div id="storeId">loading</div>
What am I doing wrong with storeId? It's empty.
If you want to match "dot" char, you have to escape it, like this:
var storeId = storeName.replace(/\./g,"").replace(/\s/g, '').replace(/'/g,"");
Here's a fiddle: https://jsfiddle.net/e63bq01L/
If not escaped, the dot matches all characters in a string.
You have to escape the dot character:
storeName.replace(/\./g,"").replace(/\s/g, '').replace(/'/g,"")
Otherwise, you will replace everything.
My backend has json string arrays, but some of the chars are escaped when send the value to frontend, like " é. How can I change all of the escaped chars to its original value? For some reason, I can't use other JS frameworks. The innerHTML seems didn't be supported in all browsers(like firefox).
You can use below snippet to unescape the string
//Snippet#1
var str="é";
var str_esc=escape(str);
document.write("Escaped String: " + str_esc + "<br>")
document.write("Original String: " + unescape(str_esc))
or
//Snippet#2
var str="é";
var enc_str = encodeURI(str);
var dec_str = decodeURI(enc_str);
document.write("Escaped String: " + enc_str + "<br>")
document.write("Original String: " + dec_str)
var str = 'let us pretend that this is a blog about gardening&cooking; here's an apostrophe & ampersand just for fun.';
This is the string I'm operating on. The desired end result is: "let us pretend that this is a blog about gardening&cooking; here's an apostrophe & ampersand just for fun."
console.log('Before: ' + str);
str = str.replace(/&(?:#x?)?[0-9a-z]+;?/gi, function(m){
var d = document.createElement('div');
console.log(m);
d.innerHTML = m.replace(/&/, '&');
console.log(d.innerHTML + '|' + d.textContent);
return !!d.textContent.match(m.replace(/&/, '&')[0]) ? m : d.textContent;
});
console.log('After: ' + str);
The problem is that HTML doesn't support XML's '
To avoid the issue you should use ' instead of '
For more information look at this post:
Why shouldn't ' be used to escape single quotes?
This should do what you want:
str.replace(/&([#x]\d+;|[a-z]+;)/g, "&$1")
or, with a positive lookahead:
str.replace(/&(?=[#x]\d+;|[a-z]+;)/g, "&")
I don't think you need any HTML2text en-/decoding.