I'm trying to get a new line on a jspdf string. Problem is firestore won't recognize it ( \n ) as a line break but as a string.
Solutions I've tried:
\n\n , html br tag
var string = "This is a sample lb\n text"
var string1 = string.replace("lb","\n")
doc.text(10,10, string1)
actual : This is a sample lb\n text
expected : This is a sample
text
The Cloud Firestore SDKs don't have an issue with new lines, so the issue must be somewhere else in your code. To demonstrate, I ran the below code that wrote a string into a document, retrieved it, then displayed the result:
Code:
db = firebase.firestore();
db.collection("lines").doc("newline").set({'myString': "This is a new\nline"});
x = db.collection("lines").doc("newline").get();
x.then(function(doc) {console.log(doc.data().myString)});
Related
I'm trying to use a string retrieved from firebase firestore and display it on my HTML page with line breaks.
In the store, I have a string that looks like this
row1\nrow2\nrow3
When I retrieve it and try to add it to my page the \n's do not register as line breaks. I am using pre-wrap and using a test-string works fine.
let text = getString(); //retrieves a string from firebase
document.getElementById('textBox').textContent = text;
Shows this on my page:
row1\nrow2\nrow3
The following test code:
let text = 'row1\nrow2\nrow3';
document.getElementById('textBox').textContent = text;
Shows the following on the page:
row1
row2
row3
So it seems like the \n's in the string retrieved from the database are not read the same way as the \n's that are put inside the string defined directly in the Javascript code. Any idea why? And what can I do to make it read the line breaks?
Similar to what #user14063792468 said in their comment, it appears your newline characters have been escaped (i.e., converts \n to \\n) when a string is stored, so I'd try to replace any instances of \\n with \n (single backslash) and see if that works.
Here's an example of how this might look before and after the replacement:
let string = 'Newline characters\\nare in this\\nstring\\nfor sure'; // note the double slashes
document.getElementById('before').textContent = string;
let text = string.replace(/\\n/g, "\n");
document.getElementById('after').textContent = text;
<pre id="before"></pre>
<pre id="after"></pre>
I've been trying to wrap my head around this whole line break thing, and I've searched and researched my soul out here. I can't seem to find an answer to my specific problem here. I want to fetch the input from a textarea and put it in an array with new lines. All it does is put a comma between the words, and it seems it only adds multiple commas to where the line breaks are supposed to be. When I add < br / >, all it does is exclude the letter b from the text.
function Wordscount() {
var pText = document.getElementById("myTextarea").value.split(/[\n <>.,\?]/);
document.getElementById("text").innerHTML = pText;
It basically just looks like this when I test it :
I am new to Javascript, and I wouldn't have gone for this solution unless this was the method our professor told us to use. I'm really frustrated here, and I'm just trying to get the hang of this.
Splitting a string turns it into an array. Treating an array as a string is equivalent to calling yourArray.join(','). Since you don't want to add commas, don't just treat the array as a string.
If you want to put HTML line breaks in, then you need to do so explicitly.
var array_of_lines = document.getElementById("myTextarea").value.split("\n");
var string_of_html = array_of_lines.join("<br>");
document.getElementById("text").innerHTML = string_of_html;
If you don't want HTML special characters to be treated as having special meaning, then convert each line to a text node and append it instead.
var array_of_lines = document.getElementById("myTextarea").value.split("\n");
document.getElementById("text").innerHTML = "";
while(var text = array_of_lines.unshift()) {
document.getElementById("text").appendChild(
document.createTextNode(text)
);
document.getElementById("text").appendChild(
document.createElement("br")
);
}
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 have a for loop in which I am printing out content on a web page. Anything with a line break in the database will not work with innerHTML.
Here is the process of how to get a string from the db:
1. Submit a form with text thats gets queried into the database.
2. For loop which displays all the contents in the database using an array,
say $content[i].
value.innerHTML works with any strings that don't have a line break.
For example, in the db:
Hello
there
----- Will not work.
Hello there
----- Will work.
I have tried using regexes to get a line break and change them to br and such, but inner html will not display content with line breaks. Only those without.
My code that I am using:
var commentSection = document.createElement("div");
var str = '<?php echo $comment['contents'];?>';
str = str.replace(/\r\n?|\n/g, '<br />');
var userCommentData = document.createElement("div");
userCommentData.innerHTML = str;
commentSection.appendChild(userCommentData);
And a more clearer picture of the error I am having:
A step of a query string
Use following functions:
htmlentities:Convert all applicable characters to HTML entities
nl2br Inserts HTML line breaks before all newlines in a string
Or maybe change your sql query with TRIM function as used in link
Use backtick character ( ` ) for multiline string in javascript.
use this line { Replace backtick with ( ` ) }
var str = backtick <?php echo $comment['contents'];? > backtick ;
insted of this line
var str = '<?php echo $comment['contents'];? >';
I am working with laravel, I have string like this in database,
hello
username, address
country
Username and country is in new line, and fields type is 'text' in database,
When i am trying to get that into JavaScript variable like this,
var add="{{ $data['order']->address }}";
Getting error unterminated string literal. No error if string in 1 line in database.
First option
You can use ES6 and the new string notation:
var str = `my
multiline
string`;
Second option
Replace newlines with \n as described here:
var add="<?=str_replace("\n", '\n', $data['order']->address)?>"