I have this url that I have to pass as a javascript string. I know that I need to escape the characters, but no matter what I do I can't seem to get it right?
var v = "www.youbigboy.com/showthread.php?t=1847";
You are using double quotes inside a string declared with double quotes. You can:
Change the double quotes inside the string to single quotes
var v = "<a href='www.youbigboy.com/showthread.php?t=1847'>www.youbigboy.com/showthread.php?t=1847</a>";
Escape the double quotes inside the string with \
var v = "www.youbigboy.com/showthread.php?t=1847";
Change the double quotes outside the string to single quotes
var v = 'www.youbigboy.com/showthread.php?t=1847';
you can use single quotes ' to differentiate internal quotes, or you can escape your internal quotes \"
The code becomes:
var v = "<a href='www.youbigboy.com/showthread.php?t=1847'>www.youbigboy.com/showthread.php?t=1847</a>";
or
var v = "www.youbigboy.com/showthread.php?t=1847";
maybe this
var url = "www.youbigboy.com/showthread.php?t=1847"; //or other dynamic url's
var sb = "<a href='" + url + "'>big boy url</a>";
source: is the + operator less performant than StringBuffer.append()
Related
I've got something like this data that comes from API and I need to do JSON.parse this, but the problem was there were inner double quotes so I can't parse it what should I do, can I use regex or something.
const dataString = '{"EN" : "2. This English "Blha Blha" "Woo Woo" something wrong."}';
I also used this regex
replace(/(^"|"$)|"/g, "'");
but by using this regex it's replace all the double quotes with single quotes like this =>
{'EN' : '2. This English 'Blha Blha' 'Woo Woo' something wrong.'};
I only want to replace the quotes like this
{'EN' : '2. This English "Blha Blha" "Woo Woo" something wrong.'};
The thing is that single quotes are not valid symbols for JSON if they are used to wrap key/values. To make it clear - you need to escape double-quote symbols in values. A better approach is to update your API with that correction. More ugly but working way is placed below:
const dataString = '{"EN" : "2. This English "Blha Blha" "Woo Woo" something wrong."}';
const normalize = (dataString) => {
let newString = dataString;
newString = newString.replaceAll(`"`, `\\\"`);
newString = newString.replaceAll(`{\\\"`, `{"`);
newString = newString.replaceAll(`\\\"}`, `"}`);
newString = newString.replaceAll(`\\\" : \\\"`, `" : "`);
return newString;
};
console.log(JSON.parse(normalize(dataString)));
var searchTerms = escape(jQuery('input#q').val());
var st = searchTerms.trim();
var res = st.replaceAll("TITLE","ti").replaceAll("%20","%20and%20").replaceAll("AUTHOR","au");
I have the above code and need the search term values in double quotes as the result
It gives result URL as : '&query=heartmate%20and%20owens'
But I need it as : '&query="heartmate"%20and%20"owens"'
The simplest way is to map the values to new values before you inject them into the request. But first you need to split the string into its individual terms...
let terms = st.split(' ');
that will return an array of the individual elements of the string, split on a space character,
then you can trim and append the term...
terms.map(term => {
term.trim(); // <-- this removes all of the whitespace characters, including
// space, tab, no-break space, and all the line terminator
// characters, including LF, CR, etc. from the beginning and end
// of the string
return '"' + term + '"';
});
You may find the need to check a condition of term before applying the map, it really depends on what you're doing.
You can use backslash \ to escape your character
var test = " \" \" ";
console.log(test);
I have received the following string from my ajax request:
As per json doc, "quotes must be escaped "
This is stored in data.description and is embedded in the template as:
''
data-title's value is used as a caption for lightbox plugin pop up. I tried the following function:
var HtmlEncode = function(s) {
var el = document.createElement("div");
el.innerText = el.textContent = s;
s = el.innerHTML;
return s;
}
as:
''
Now since the data.description contains multiple quotes javascript assumes them as multiple argument and throws error. I searched many other Stackoverflow posts which suggest to append the data in a div and retrieve its inner HTML but that is possible in my case.
Thanks
Change only the quotes that are the same as those will be surrounding the data-title. Like this:
var description = data.description.replace(/"/g, "'");
var template = '';
You can replace the double quotes in your string with single quotes or any other symbol in order for it to work inside double quotes.
you can do this with String.replace(/["]+/g,"'") this will replace the double quotes in the string with a single quote, the g in the reg-ex tells it to use all the string.
you can also fake it by using 2 single quotes like this String.replace(/["]+/g,"''"), and it will fake double quotes for you.
var HtmlEncode = function(s) {
return s.replace(/["]+/g,"''");
}
Solution 1
Escape it with replace and a regexp:
var HtmlEncode = function(s) {
return s.replace(/"/g, '\\"').replace(/'/g, "\\'");
}
''
Solution 2
Let jQuery doing this for you (only if you are using it).
var tpl = $('');
tpl.data('title', data.description);
Try to replace the quotes with HTML entity equivalents after which you can then safely use it on attributes.
var desc = data.description.replace(/'/g, "'").replace(/"/g, """);
''
Here's a Fiddle to demonstrate the solution.
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="\"\'"
I would like to do something like this:
s = s.replace(/(”)/g, '"'); // need to replace with double quotes
s = s.replace(/(“)/g, '"'); // need to replace with double quotes
s = s.replace(/(’)/g, "'"); // need to replace with single quotes
But for me this does not work. I tried all the ways.
You can use Unicode values in replace:
s = s.replace(/\u201C|\u201D/g, '"'); // for “ or ”
s = s.replace(/\u2019/g, "'"); // for ’
DEMO: http://jsfiddle.net/uM2fY/
So we open console, and see:
>>> 'test&rqduo;foo'.replace(/&rqduo;/g, '\"' );
test"foo
and with braces:
>>> 'test&rqduo;foo'.replace(/(&rqduo;)/g, '\"' );
test"foo
Everything work just like you thought. Please, check your input strings.