QuizCure
Find Available Solution Here!

Javascript Replace Comma with NewLine

Deepak Dec 08, 2024

There are numerous ways in javascript to replace characters from a given string. It depends on the scenario to choose a method to perform substring replacement.

We will go through some of the javascript replace the comma with newline methods.

Replace comma with newline using split & join function

<script>
var content = "Hello, there";
var dataArray = content.split(",");
console.log(dataArray);
console.log(dataArray.join("\n"));
 </script>

Result:


 ['Hello', ' there']
Hello
 there

Code Explanation::

  • Let suppose we have a comma-separated string "Hello, there". Here we defined Javascript variable content to hold string.
  • Javascript function split is used to divide comma separated content into an array of substrings. content.split(",") return array ['Hello', ' there']
  • Now we have an array of substrings. Next, we need to join array elements by using the javascript function join to convert back to a string.
  • The new line character \n is used as a Separator to join array elements to make a string.
  • If we require HTML line break then we need to use
    instead of \n

In the same way we can do following replacement

  • Replace comma with space
  • Replace comma with HTML line break
  • Replace comma with \t
  • Replace character in string

Explore Regex replace comma using javascript function replace

In the previous example we learned to replace commas using split and join functions. It was required to do multiple manipulations to retrieve desired results.

But we have a replace function that supports regular expressions to search for patterns and replace characters accordingly.

Lets see the following example program to replace commas using replace function.

<script>
var employee = "Peter, Alex, John, Camren";

employee_list = employee.replace(/,/g, '\n');

console.log(employee_list);
 </script>
   

Result:

Peter
 Alex
 John
 Camren
 

Code Explanation:

  • Lets suppose we have a comma-separated employee string. Variable employees store this string.
  • Here we use regular expressions with the g modifier to replace all occurrences of commas in a given string. [What if we do not use the g modifier in search terms? Please refer to below example for this]
  • employee.replace(/,/g, '\n') return comma replaced string from given employee names.
  • Each name is printed here in newline due to replace character \n

Replace function will replace the first occurrence of matched character from content if the regular expression is not provided.

Therefore the following program only replaces the first finding search character.

<script>
var employee = "Peter, Alex, John, Camren";
employee_list = employee.replace(/,/, '\n');
console.log(employee_list);

 </script>

Result:

Peter
 Alex, John, Camren
    

Was this post helpful?

Send Feedback

Connect With QuizCure


Follow Us and Stay tuned with upcoming blog posts and updates.

Contributed By

Deepak

Deepak

QC STAFF
51 Posts
  • PHP
  • JAVA
  • PYTHON
  • MYSQL
  • SEO

You May Like to Read

Scroll up