regex - javascript - remove some text from string
New here? Learn about Bountify and follow @bountify to get notified of new bounties! x

I have a ton of text in a string and i want to get rid of some specific ughly things in it. I would like to get a javascript demo using regex to remove this junk

  • \n
  • \t
  • any double or more spaces 1 can be replaced with a single space
  • wrapped relative urls {\"hook\":\"/content/en/visit/fall-fashion/jcr:content/mainpar/campaignhero\"}

here is a link to some sample input

your demo can just be a jsfiddle or codepen of a textbox that i can paste in some string like this and have it come out pretty on the other side.

awarded to julianobs

Crowdsource coding tasks.

3 Solutions

Winning solution

Is this ok?

text = text.replace(/\n|\t/g,"").replace(/ +/g, " ").replace(/\{"hook":"[^"]+"\}/g, "")

was perfect
Qdev 3 years ago

In the solution, some double whitespace is still present, and the start and end of the text are not trimmed properly. Here is a modified solution:

text = text.replace(/\{"hook":"[^"]+"\}/g, "").replace(/\s+/g, " ").trim();

View Timeline