1

You can make an array from a string and remove a certain character with split:

const str = "hello world"
const one = str.split(" ")
console.log(one) // ["hello", "world"]

But how can you split a string into an array without removing the character?

const str = "Hello."
["Hello", "."] // I need this output
Evanss
  • 23,390
  • 94
  • 282
  • 505

1 Answers1

0

While you can use a capture group while splitting to keep the result in the final output:

const str = "Hello.";
console.log(
  str.split(/(\.)/)
);

This results in an extra empty array item. You should probably use .match instead: either match .s, or match non-. characters:

const match = str => str.match(/\.|[^.]+/g);

console.log(match('Hello.'));
console.log(match('Hello.World'));
console.log(match('Hello.World.'));
CertainPerformance
  • 356,069
  • 52
  • 309
  • 320