Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to break to a new line when string contains "↵"? [duplicate]

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?

like image 609
Wayne Li Avatar asked Aug 03 '16 01:08

Wayne Li


4 Answers

Try to use replace function. It's pure javascript.

text.replace(/\u21B5/g,'<br/>')

21B5 is the unicode for

like image 180
levi Avatar answered Sep 26 '22 06:09

levi


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;

like image 37
MaieonBrix Avatar answered Sep 23 '22 06:09

MaieonBrix


var newString = mystring.replace(/↵/g, "<br/>");
alert(newString);

You can find more here.

like image 30
Aleksandar Đokić Avatar answered Sep 26 '22 06:09

Aleksandar Đokić


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]); 
}
like image 34
Doua Beri Avatar answered Sep 25 '22 06:09

Doua Beri