I got a string like this var text = "aaaaaaa↵bbbbbb↵cccccc"
from the php server, and I want to output this string to the screen.
When there is a "↵", the following text goes to a new line. I am using AngularJS.
How can I achieve this by using plain Javascript or through AngularJS?
Try to use replace function. It's pure javascript.
text.replace(/\u21B5/g,'<br/>')
21B5
is the unicode for ↵
You can easily do that by doing a regex:
var text = "aaaaaaa↵bbbbbb↵cccccc";
text.replace(/↵/, '<br/>');
this will only replace the first one, by adding the g (global) parameter we will replace any occurence of this symbol, we simply put the g after the / like so
text.replace(/↵/g, '<br/>');
Basically here we store the data in a variable called text then we use the string/regex method called replace on it .replace() that takes two parameters: the pattern to search and with what we are going to replace it;
var newString = mystring.replace(/↵/g, "<br/>");
alert(newString);
You can find more here.
Use str.split([separator[, limit]])
: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split
var text = "aaaaaaa↵bbbbbb↵cccccc";
for(var i=0;i<text.split('↵').length;i++){
// use text[i] to print the text the way you want
// separated by <br>, create a new div element, whatever you want
console.log(text[i]);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With