I want to replace semicolon with new line.
ie 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
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"
There are a few different things going wrong for you:
This is the default for JavScript. A regex will replace the rest.
X.content.replace(/;/g, '\n')
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.