i have this text:
this is a sample
text
Apparently, after "sample" there is a Line feed character (ENTER). How can i encode JUST those characters and not the whole string. When i use encodeURI the whole string gets encoded
What i want to do is to get a string like "this is a sample%0Atext"
Thanks
So double url encoding - where the API requires raw input of %250A for a \n ?
str = str.replace(/[\r\n]/g, function(m) { return escape(m); });
Related
If a string contains character from interval U+D800..U+DFFF then encodeURIComponent() throws a malformed URI sequence error. I would like to eliminate those characters from a given string before passing it to encodeURIComponent(). How to do that?
Example:
I have a textfile encoded in UTF-16BE which contains the following hexa chars:
D7FF D800 D801 ... DFFE DFFF E000
I'm searching for a function which returns this string from the string above:
D7FF E000
So only valid Unicode characters remain.
You can use a replace/encodeURIComponent combo to achieve the desired result. You first need to match all the characters that do not fall in the unicode range [0xD800..0xDFFF] using this regex: /[^\uD800-\uDFFF]+/g then replace them with their encoded versions:
let result = string.replace(/[^\uD800-\uDFFF]+/g, match => encodeURIComponent(match));
Example:
let string = "/foo/\uD7FF\uD800\uD801/bar";
let result = string.replace(/[^\uD800-\uDFFF]+/g, match => encodeURIComponent(match));
console.log(result);
Why is this giving me this error?
<script>
function loadImg() {
var imageChosen = document.getElementById('imageChosen').value.replace("C:\fakepath\","");
alert(imageChosen);
document.getElementById('image').src = imageChosen;
}
</script>
I expect the image with id "image" to show the chosen image.
The value in your call to replace() is not escaped properly.
The value should instead be:
"C:\\fakepath\\",""
Read more about escaping strings here
The problem is due to the escape string character \ (backslash)
When using strings in Javascript we may escape some character in the string. For example a break line (\n) or even a "(double quotes) when declaring the string or even an backslash \ need a escape.
Examples:
x = "my \\" // Will output as the same as "my \"
z = "my \"quotes\" // Will output as 'my "quotes" '
I have this JSON string:
{\"text\":\"Line 1\\nLine 2\",\"color\":\"black\"}
I can parse it when I do this:
pg = JSON.parse(myJSONString.replace(/\\/g, ""));
But when I access pg.text the value is:
Line 1nLine 2.
But I want the value to be exactly:
Line 1\nLine 2
The JSON string is valid in terms of the target program which interprets it as part of a larger command. It's Minecraft actually. Minecraft will render this as you would expect with Line 1 and Line 2 on separate lines.
But I'm making a editor that needs to read the \n back in as is. Which will be displayed in an html input field.
Just as some context here is the full command which contains some JSON code.
/summon zombie ~ ~1 ~ {HandItems:[{id:"minecraft:written_book",Count:1b,tag:{title:"",author:"",pages:["{\"text\":\"Line 1\\nLine 2\",\"color\":\"black\"}"]}},{}]}
Try adding [1] at /\[1]/g but works for single slash only, but since the type of the quoted json i think is a string when you parse that it slash will automatically be removed so you don't even need to use replace. and \n will remain as.
var myString ='{\"text\":\"Line 1\\nLine 2\",\"color\":\"black\"}';
console.log(JSON.parse(myString.replace(/\\[1]/g, ""))); //adding [1] will remove single slash \\n -> \n
var myString =JSON.parse(myString.replace(/\\[1]/g, ""));
console.log(myString.text);
Your string is not valid JSON, and ideally you should fix the code that generates it, or contact the provider of it.
If the issue is that there is always one backslash too many, then you could do this:
// Need to escape the backslashes in this string literal to get the actual input:
var myJSONString = '{\\"text\\":\\"Line 1\\\\nLine 2\\",\\"color\\":\\"black\\"}';
console.log(myJSONString);
// Only replace backslashes that are not preceded by another:
var fixedJSON = myJSONString.replace(/([^\\])\\/g, "$1");
console.log(fixedJSON);
var pg = JSON.parse(fixedJSON);
console.log(pg);
I'm trying to decode some specail characters in my string, before input it in HTML. But, for some reason, it didn't work.
For ex:
My input string is "ampere 13th\'."
In JS I'm replacing every special character with this function:
htmlEntities: function(str) {
return str.replace(/\\/g, "\").replace("'", "'").replace(".", ".").replace("%", "%").replace("\"",""");
},
But, when put it to HTML, it still looks like :
"ampere 13th\'."
I want to show my data with replaced special characters.
What I'm doing wrong?
There is a single backslash in your string which is not being recognized as '\' but instead its an escape sequence. Other characters are being replace perfectly. I have written the following function which alerts the output string accordingly.
function test() {
var str = "ampere 13th\'.";
alert(str.replace(/\\/g, "\").replace("'", "'").replace(".", ".").replace("%", "%").replace("\"", """));
}
And it alerts me
ampere 13th'.
which is correct except replacing the '\' character. if you want to replace the '\' you can further search on how to replace a backslash character in java script.
if I have my input string like this
var str = "ampere 13th\\'.";
with two backslashes then the replacement occurs perfectly and my function alerts me
ampere 13th\'.
I have a string of url encoded characters.
wondering if there is a javascript function that can replace all the url encoded characters with normal string characters.
i know i can use the replace function, but it only replaces one certain character an not all at once
for example, I am looking for one function that will replace all the url encoded characters in this string:
string urlEncoded = '1%20day%20%40work!%20Where%20are%20you%3F'
and give me this string:
string replaced = '1 day # work! Where are you?'
thanks a lot
Use decodeURIComponent(string) for that purpose.
string urlEncoded = '1%20day%20%40work!%20Where%20are%20you%3F';
string replaced = decodeURIComponent(urlEncoded);
alert(replaced);
More info here: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/decodeURIComponent
string replaced = decodeURIComponent(urlEncoded);
There is also just decodeURI but this does not cope with "special" characters, such as & ? ! # etc
Use decodeURIComponent(urlEncoded)
You are looking for unescape
var decoded = unescape(urlEncoded);