1

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');`
3
  • why you can not insert <br> in your mysql fields? a personal reason or grammatically? Commented Apr 15, 2015 at 15:55
  • I have tried \n, <br>, as well as them in quotes. However, I believe it is because I am using a SQLite manager for web apps and instead of doing line breaks it instead shows the actual text ie <br> on the front-end. Commented Apr 15, 2015 at 16:01
  • The code up there looks like you are replacing treat.Tips after you added that string the DOM? Just updating treat.Tips is not going to update the DOM. You have to call $("#tips").append("<li class='treatment'>" + treat.Tips + "</li>"); after you run your regex Commented Apr 15, 2015 at 16:05

2 Answers 2

3

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 periodand 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');`
Sign up to request clarification or add additional context in comments.

1 Comment

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!
1

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 />");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.