0

I have a string in jquery like following:

var str = "\n    \n        \n        \n            Michael Hartl\n        \n    \n";

but I want to fetch this string into this format as output

str = "Michael Hartl"

I tries with following functions:

str.replace(/\s*/," ")

but it results like

 " Michael Hartl


"

is there any other way in jquery to get the desired output?

Mosh Feu
  • 28,354
  • 16
  • 88
  • 135
Ravi Kumar SIngh
  • 311
  • 1
  • 4
  • 17
  • 6
    `str.trim()`: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/Trim. Better still, fix the source of this problem to not allow junk to be entered. – Rory McCrossan Jun 12 '19 at 08:14
  • `str.trim()` is the way, but if you had a string like `str = "word \n \n some more words \n \n bla bla"` and wanted to get rid of whitespace, `str.trim()` wouldn't work and you were close to your RegExp, the good call would be `str.replace(/\s+/g, " ")` – Silviu Burcea Jun 12 '19 at 08:23
  • @ Rory McCrossan thank .trim() worked – Ravi Kumar SIngh Jun 12 '19 at 09:27

0 Answers0