-1

Hi i have a data scraped from a website and i want to clean its paragraph and i dont how can i remove multiple underscore and replace it with space or new line.

Here is the sample paragraph I scraped using cheerio.js

The future of mobile gameplay is War Games!_______________________________________ATTENTION ALL PLAYERS! We’d love to hear your feedback to help us improve the game. To leave feedback visit here.

Then I want this to be like this.

The future of mobile gameplay is War Games!
ATTENTION ALL PLAYERS! We’d love to hear your feedback to help us improve the game. To leave feedback visit here.
elpmid
  • 630
  • 3
  • 12

1 Answers1

2

const text = "The future of mobile gameplay is War Games!_______________________________________ATTENTION ALL PLAYERS! We’d love to hear your feedback to help us improve the game. To leave feedback visit here.";

let newstr = text.replace(/\_+/i, "\n");
console.log(newstr);

The \_+ part will match one or more "_" and replace everything with a newline.

Osama Sayed
  • 1,787
  • 12
  • 15
  • can i have a follow up question on this? how can i add this '\n' to that regex. – elpmid Sep 28 '20 at 06:18
  • 1
    Good answer. BTW an underscore is not a special character in regex and you do not need to espace it with a backslash. The `i` option will be unnecessary as well. It may be better to add `g` option considering the case the sentense contains multiple sequences of underscores. Cheers. – tshiono Sep 28 '20 at 06:42