0

I have string which is returning this to me:

<div class=\"sqs-layout sqs-grid-12 columns-12\" data-layout-label=\"Post Body\" data-type=\"item\" data-updated-on=\"1539277027193\" id=\"item-5bbf7e3f9140b70d3962e2a0\"><div class=\"row sqs-row\"><div class=\"col sqs-col-12 span-12\"><div class=\"sqs-block image-block sqs-block-image\" data-block-type=\"5\" id=\"block-yui_3_17_2_1_1539794362468_25479\"><div class=\"sqs-block-content\">\n\n  \n\n  \t\n      <div class=\"image-block-outer-wrapper">

Any idea how I can remove all the whitespaces and put this into a JS variable? I have tried countless variations of this:

.replace(/[\n\t\r]/g,"")

but nothing working yet...

Liam
  • 27,717
  • 28
  • 128
  • 190
dwinnbrown
  • 3,789
  • 9
  • 35
  • 60

1 Answers1

1

You have to assign the value after the replace to another variable or the same variable. This is because strings are immutable. Also add \s on the regex pattern.

var a = "\n\n  \n\n  \t\n      "
var b = a.replace(/[\n\t\r\s]/g, '');

//now b = "" and a still is "\n\n  \n\n  \t\n      "
a = a.replace(/[\n\t\r\s]/g, '');  // this also work
atiqorin
  • 446
  • 3
  • 13