Reputation: 609
I am looking for Javascript expertise to be able to find specific words after mySQL outputs my text. I have numerous fields in a column in my phpmyadmin database with sentences such as: Bobby ran down the hill. He saw nothing. Bobby went home.
I would like to create a javascript code that finds the periods .
and be able to replace them with html code <br>
because I cannot add a line break in the mySQL fields even though I've tried concatenation. Help on this issue would be appreciated!
Here is my script:
$("#tips").append("<li class='treatment'>" + treat.Tips + "</li>");
$("#tips-text").text(treat.Tips);
treat.Tips = txt.replace(/\./g, "<br />");
$('#tips').listview('refresh');`
Upvotes: 1
Views: 124
Reputation: 5745
The Javascript replace can do that with the help of regular expressions: the method normally would replace one instance of the found text, but if you user regular expressions, it can replace all instances
I used the regex /\./g
: this means find any period
and the g
modified means that the search is global for all the text.
var txt = "Bobby ran down the hill. He saw nothing. Bobby went home.";
txt = txt.replace(/\./g, "<br />");
DEMO: http://jsfiddle.net/sj33gg07/
Thank you for your answer. I am pulling treat.Tips from phpmyadmin that contains my data. Do you have any suggestions on how to further display replace? I revised my code par your suggestion but it broke my script. Please see my revised question for the code, thanks! – chronotrigga 4 mins ago
You should do the append after you replace not before
treat.Tips = treat.Tips.replace(/\./g, "<br />");
$("#tips").append("<li class='treatment'>" + treat.Tips + "</li>");
$("#tips-text").text(treat.Tips);
$('#tips').listview('refresh');`
Upvotes: 3
Reputation: 1454
You can use the replace method and look for the dots and replace it for </ br>
Here is an example:
var text = "Bobby ran down the hill. He saw nothing. Bobby went home.";
text = text.replace(/\./g,"<br />");
Upvotes: 1