

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How do I split a string, breaking at a particular character in JavaScript?
To split a string on every occurrence of ~, split the array. After splitting, add a line break i.e. <br> for each occurrence of ~.
For example,
This is demo text 1!~This is demo text 2!~~This is demo text 3!
After the split and adding line breaks like the following for ~ occurrence −
This is demo text 1! This is demo text 2! This is demo text 3!
Example
Let’s see the complete example
<!DOCTYPE html> <html> <body> <h2>Adding line break</h2> <script> var myArray = 'This is demo text 1!~This is demo text 2!~ ~This is demo text 3!~This is demo text 4!~ ~This is demo text 5!'; document.write("Original Array: " +myArray); var brk = myArray.split('~'); var res = brk.join(" <br> "); document.write("<br><br>"+res); </script> <p>Each occurrence of ~ adds a line break above.</p> </body> </html>
- Related Questions & Answers
- How do I replace a character at a particular index in JavaScript?
- How do we split a string on a fixed character sequence in java?
- How do I split a multi-line string into multiple lines?
- Split a string around a particular match in Java
- How to remove a particular character from a String.
- How do I remove a particular element from an array in JavaScript
- Insert a character at nth position in string in JavaScript
- How important is backslash in breaking a string in JavaScript?
- How do you convert a string to a character array in JavaScript?
- How do I split a numerical query result in MySQL?
- How do I search through an array using a string, which is split into an array with JavaScript?
- How do I convert a string into an integer in JavaScript?
- How do I split the letters and numbers to two arrays from a string in PHP
- How to remove all text from a string before a particular character in R?
- How do I display the content of a JavaScript object in a string format?
Advertisements