I have a question:
I have a QR Code Scanner which gets a VCard. Im getting back the following String:
BEGIN:VCARD VERSION:2.1 FN:John Doe N:doe;john END:VCARD
The problem is the following: When putting this Information (via Javascript) in a textarea or just dump it to an alert winow, the information has breaks in between, looking like this:
BEGIN:VCARD
VERSION:2.1
FN:John Doe
.
.
.
But when putting this information into a paragraph (<p></p>), it has no breaks in it and it's just a plain string.
The question is now, can I put those breaks into the paragraph as well or can I insert another sign or anything else in between the attributes?
I thought about just splitting the string on blanks, but it's not working because e.g. FN itself contains blanks as wel...
Thanks for your help.
Edit:
Unfortunatelly, all 3 advices don't work..
What I have now is the following:
function writeqrcodecontent(){
var textfeld = document.getElementById("inhaltvonqrcode");
var wert = $.scriptcam.getBarCode();
//wert.split('\n').join('<br/>');
//wert.replace("\n", "<br>");
//wert.replace(/\n/g,'<br>');
textfeld.innerHTML = wert;
textfeld.style.display="block";
}
But as said, all three commented out lines do not work, everything is still displayed in one line.
The paragraph is defined with:
<p id="inhaltvonqrcode" style="display:none; clear:both;"></p
I understand your ideas but I don't understand why it's not working...
By the way, what I also tried is something like wert.split('\n').join('#'); to see, if it's really the break or it's just not working, but this doesn't work as well, so the text just seems to have no \n in it..
EDIT 2
It's working now, I neeeded to do both steps in one step, so not
var wert = $.scriptcam.getBarCode();
wert.split('\n').join('<br/>');
but
var wert = ($.scriptcam.getBarCode()).split('\n').join('<br/>');
Thanks for your help!
The string you get back likely contains newline characters \n. To get them to display with the newlines intact in HTML, you can either wrap everything returned with <pre></pre> in your HTML. Or split on newline \n and replace it with <br />.
text.split('\n').join('<br />')
text.replace(/\n/g,'<br>');
this line will replace all lines with <br />
Your barcode reader is reading the vCard with \n newline, however the pure html this newline is ignored.
in Javascript you can just use something like
someText.replace("\n", "<br>");
and it will do what you want. In php its the nl2br function
Related
I want to replace semicolon with new line.
i.e. whenever semicolon applears it has to move to new line.
I tried .replace(). It was not working for new line but was working for some other string. One more problem is only the first semicolon will be replaced.
Here is my code snippet:
<p>{{X.content.replace(';', '\n')}} </pre>
This was not working, then i tried with:
<p> {{X.content.replace(';', 'REPLACE')}} </p>
The code was working with any other string but only the first semicolon (;) will be replaced.
I want this to be converted and appear in the new line
first line; second line; third line;
You need a regex with global flag to change all instances
X.content.replace(/;/g, '\n')
this is not something you should be doing in the view. Suggest you create a filter or modify the data in controller
There are a few different things going wrong for you:
Only thie first semicolon is replaced:
This is the default for JavScript. A regex will replace the rest.
X.content.replace(/;/g, '\n')
The newlines don't show up:
This is because HTML does not respect newlines by default. You seam to have realised this because you have writen <p></pre> (which is odd and broken).
Either:
Replace ; with \n and then use a well formed pre:
<pre>{{X.content.replace(/;/g, '\n')}}</pre>
Or:
Replace ; with <br /> and then declare the result as html:
(Warning: this option leaves you open to injection attacks so you need to make sure you trust the content)
<p ng-bind-html="X.content.replace(/;/g, '<br />')"></p>
EDIT:
Probably a better approach for you would be to forget all that and just do:
<p ng-repeat="line in X.content.split(';')">{{line}}</p>
This is less risky and results in nicer HTML to style as you choose.
try like this here is one working example
var x ="dsdfs;sfsf;fdsfs"
console.log(x)//"dsdfs;sfsf;fdsfs"
var y =x.replace(/;/g, " \n ")
console.log(y)
//"dsdfs
sfsf
fdsfs"
I'm currently developing a simple web app using html with JavaScript, and I'm trying to do a simple string.replace call on a string received from a html textarea like so;
var contents = document.getElementById("contents").value;
var alteredText = contents.replace(/£/g, "poundsign");
The problem is that when a £ sign is included in the string, the replace call can't find it. I've looked at the code via the console and it seems that anytime there's a $ sign in JavaScript it adds a "Â" to the £ symbol, so
string.replace(/£/g, "poundsign");
as it was written in the js file becomes the following while running:
string.replace(/£/g, "poundsign");
while £ in var contents remains simply £ (putting £ into the textarea causes the replace call to work correctly). Is there a way to stop the  being added in the js file, or to add it to the html file before .replace is called?
The  is added anytime £ appears in the js file as far as I can see, and I haven't been able to get it to match up with the html without the user adding the  to the html themselves.
// replace pound string by empty string
var mystr = '£';
mystr = mystr.replace(/£/g,'poundsign');
alert(mystr);
Thanks to #David Guan for the link, that put me on the right track, and to everyone else that commented.
The issue was resolved when I used the Unicode number in the .replace call rather than the character, it was able to then match the £ sign correctly without the  also being inserted.
In a small forum, any user can save posts. Sometimes those posts include words surrounded by quotes ( " " ). This gives me an error when I try to handle those strings with javascript.
I wrote some jquery code that uses the django variable like this:
new_text = "{{text|safe}}";
$("#text_p").text(new_text);
if I mark it as "safe" then javascript gives me a syntax error:
the text "(error here)word between quotes" the user posted
This is logical because javascript understands the quotes like this:
new_text = "this is the text "word between quotes" the user posted"
So, if I don't mark it as "safe" and let django escape the text, it doesn't give me an error, but the text looks like this:
the text "word between quotes" the user posted
I don't know what to do, and I guess it may not be simple cause if I use single quotes to declare the javascript variable, I will have the same problem when the user posts a text with single quotes. If I use a regex to replace double quotes and not mark the text as "text|safe", then other tags will be escaped and the text will be full of "<br />" etc.
I have an idea that may work but is ugly and probably not the best option:
including the text in a <p class = "hidden"> tag and then calling it using jquery.
So, the question is, how do I solve this?, is there a better way?
Thanks in advance for your help.
EDIT:
I created a Runnable to explain it better.
Use escapejs filter.
Example:
{{ string|escapejs }}
Ok, I found a partial solution, hope it helps someone in the future. It is not an elegant solution, so, if anyone has a better option, it will be welcomed.
I included the text that has a "quoted" word inside a html hidden tag.
python-django:
text_with_quotes = 'this is a text and a word between "quotes"'
html:
<p id = "new_text" class = "hidden"> {{text_with_quotes|safe}}</p>
js:
new_text = $("#new_text").text();
$("#text_p").text(new_text);
it works. But there may be a better option using javascript and/or python.
I have a service that is returning a message. That message may be a combination of plain text or an html formatted text.
ex1: "This is a message"
ex2: "<p> This is also a message <p/>"
ex3: "This is also a <strong> message </strong>"
The thing we would like to do is come up with a script that would return as much plain text up until the first tag. So in the examples above:
would return "This is a message.
would return ""
would return "This is also a"
I am not sure what approach is the best to do this. Can i accomplish this using Regex or JS. I know Regex can easily return text between two tags, but what i am looking for is a little different. Thanks in advance for any advice or help.
The simplest solution would be to match anything except <s, starting at the beginning of the string:
match = subject.match(/^[^<]*/)[0];
This fails if <s could occur in comments/quoted strings before the first HTML tag, but that might not be a problem.
Test on JSFiddle
Explanation:
^ # Anchor the match to the start of the string.
[^<] # Match any character that's not a <
* # zero or more times (as many as possible).
I'm writing a Photoshop Javascript script file. For all intents and purposes, this script when ran replicates a specific text layer several times. If the original text layer contains an apostrophe, the replicated instances replace the apostrophe with a square block. So "It's" becomes "It[]s" (obviously not brackets, but the square block.)
Here is the code:
titleLayer = al.textItem.contents;
newTitleLayer = titleLayer.replace("'", "\'");
alert(newTitleLayer); // At this point, this works: "It's"
persistentSetting.putData(0,newTitleLayer);
app.putCustomOptions("text_contents4",persistentSetting,true);
alert(persistentSetting.getData(0)); // At this point, it does not. It shows the square. "It[]s"
I know this has to be a simple issue, I've just never encountered this before.
Thanks.
I guess you want
newTitleLayer = titleLayer.replace(/'/g, "\\'");
// ^^^^ ^
// regex to match *all* apostrohpes escape the backslash
What ended up working for me is:
persistentSetting.getData(0).replace("EM", "'"); // It's not actually EM, but that's the little code that JS shows in my editor when I copied and pasted that special block [] character in.
Thanks for the help.