How to split the string at a specific character in JavaScript?

Published December 9, 2020

To split a string at a specific character, you can use the split() method on the string and then pass that character as an argument to the split() method in JavaScript.

/* Split string at specific character */
str.split(",");

Consider a string with commas after every word like this,

// a string
const str = "John,Doe,Mary,Roy,Lily";

Now let's use the split() method on the string and pass the character in our case comma (,) as an argument to the method to split the string when it encounters the comma.

It can be done like this,

// a string
const str = "John,Doe,Mary,Roy,Lily";

// split string at comma
// using the split() method
const splitArr = str.split(",");

console.log(splitArr); // ["John", "Doe", "Mary", "Roy", "Lily"]
  • The split() string method returns an array of split strings.
  • You can also pass regex expressions to the split() method.

See this example live in JSBin.

Feel free to share if you found this useful 😃.


Share on: Facebook Twitter