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"
Related
::head
line 1
line 2
line 3
::content
content 1
content 2
content 3
How do I get "head" paragraph(first part) text with regex? This is from txt file.
Unfortunately, the below doesn't work in javascript because of this: Javascript regex multiline flag doesn't work. So we have to tweak things a bit. A line break in a file can be found in javascript strings as \n. In windows this includes \r but not in linux, so our \s* becomes more important now that we're doing this without using line-ending characters ($). I also noticed that you don't need to specifically gather the other lines, since line breaks are being ignored anyway.
/(::head[^]*?)\n\s*\n/m
This works in testing in Chrome, so it should work for your needs.
this is a little fancy, but it should fit if this is used in conjunction with many similar properties.
/(::head.*?$^.*?$)^\s*$/m
Note that you need the /m multiline flag.
Here it is tested against your sample data http://rubular.com/r/vtflEgDdkY
First, we check for the ::head data. That's where we start collecting information in a group with (). Then we look for anything with .*, but we do so with the lazy ? flag. Then we find the end of the line with $ and look for more lines with data with the line start ^ then anything .*? then the line end $ this will grab multiple lines because of the multiline flag, so it's important to use the lazy matching ? so we don't grab too much data. Then we look for an empty line. Normally you just need ^$ for that, but I wanted to make sure this would work if someone had stuck a stray space or tab on the lines in between sections, so we used \s* to grab spaces. The * allows it to find "0 or more" spaces as acceptable. Notice we didn't include the empty line in the group () because that's not the data you care about.
For further reading on regex, I recommend http://www.regular-expressions.info/tutorial.html It's where I learned everything I know about regex.
You can use [\s\S]+::content to match everything until ::content:
const text = ...
const matches = text.match(/^([\s\S]+)::content/m)
const content = matches[1]
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
I'm trying to make a parser for formatting JavaScript in a contextual format. First I want to be able to convert the input JavaScript into one line of JavaScript and then format the code based on my requirements. This does not remove all of the enters or white space:
txt = $.trim(txt);
txt = txt.replace("\n", "");
How can I convert the text into one line?
Use a regular expression with the "global" flag set:
txt.replace(/\n/g, "");
However, you should be careful about removing linebreaks in Javascript. You might break code that was depending on semicolon insertion. Why don't you use an off-the shelf parser like Esprima?
Use :
\s character that represents any space character (Carriage return, Line Feed, Tabs, Spaces, ...)
the "greedy" g flag.
var text = txt.replace(/\s+/g, ' ');
Hope it helps
If the text comes from some operating systems, it may have the \r\n line ending, so it is worth removing both...
You should also use /\r/g this replaces ALL \rs not just the first one.
var noNewLines = txt.replace(/\r/g, "").replace(/\n/g, "");
You have to be pretty sure there are no single-line comments and that there are no missing semi-colons.
You can try to minify your code, using something like https://javascript-minifier.com/
however this will also change your variable names
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.
What is the difference between document.write(‘hello world\n’); and document.writeln(‘hello world’);?
Edit
My question is what will be the difference of output.
Historically, writeln was intended to handle different newline conventions, of with \n is only one.
There are different conventions for end-of-line. '\n' is the end-of-line marker on UNIX, '\r' on Mac (AFAIK not any more as it's now a UNIX) and '\r\n' is DOS/Windows. Using writeln should automatically use the correct one on the desired platform in other languages, but I don't really know whether JavaScript's document.writeln automatically uses the correct one automagically.
The writeln() method is identical to
the write() method, with the addition
of writing a newline character after
each statement.
from w3schools.com
edit: for the sake of completeness :)
writeln on mozilla.org
writeln on w3.org
In theory, writeln() appends a line feed to the end of your string.
In practice, since you're talking Javascript, there's little real difference if you're generating HTML, since HTML ignores extra white space anyway.
afaik there's no difference between them. You'll end up with a line, which has a "new-line-sign" at the end, so the next text youre gonna display will show up in the following line.
if this is a tricky question sorry for my ignorance:)
document.write() writes to the document without appending a newline on the end.
document.writeln() writes to the document appending a newline at the end.
Lets take this is as an example:
Out put for write():
Hello World!Have a nice day!
Note that write() does NOT add a new line after each statement.
Output for writeln():
Hello World!
Have a nice day!
Note that writeln() add a new line after each statement.
document.write you will get the courser in the same line, but in document.writeln you will get the courser in the next line.