2

Possible Duplicate:
How do I trim a string in javascript?

I have below string which comes from ajax response

"\r\n\r\n\r\n\r\n\r\n\r\n\r\n\t\r\n\tERROR: Profile : NOT SUCCESS\nCODE        : 2\nCATEGORY    : TECHNICAL\nSEVERITY    : null\nENVIRONMENT : DEV\nAPPLICATION : DEV\nDESCRIPTION : Profile: INVOCATION UNHANDLED EXCEPTION [null]\nDESCRIPTION : Profile: [ServiceAttribute]\nDESCRIPTION : Profile: Instance ID = 20130108124231841\n\r\n\r\n"

I am using below code to trim the string on both ends.

var text = originalRequest.responseText.replace(/^\s+|\s+$/g, '');

However it's removing \n in between message which is coming from ajax response. What i would like to have in the end is

"ERROR: Profile : NOT SUCCESS
CODE        : 2
CATEGORY    : TECHNICAL
SEVERITY    : null
ENVIRONMENT : DEV
APPLICATION : DEV
DESCRIPTION : Profile: INVOCATION UNHANDLED EXCEPTION [null]
DESCRIPTION : Profile: [ServiceAttribute]
DESCRIPTION : Profile: Instance ID = 20130108124231841"

how do i get this? Trying different ways from past 1 hour :(

Community
  • 1
  • 1
RaceBase
  • 18,428
  • 47
  • 141
  • 202
  • If you have JQuery, you could use [`$.trim()`](http://api.jquery.com/jQuery.trim/). But dont include JQuery just for this... – nfechner Jan 08 '13 at 11:51
  • 2
    What you have should not modify the new-lines within the string; http://jsfiddle.net/dmwpb/ make sure you view the output in something that honours new lines. – Alex K. Jan 08 '13 at 11:58

3 Answers3

5

Just use trim();:

var s = "\r\n\r\n\r\n\r\n\r\n\r\n\r\n\t\r\n\tERROR: Profile : NOT SUCCESS\nCODE        : 2\nCATEGORY    : TECHNICAL\nSEVERITY    : null\nENVIRONMENT : DEV\nAPPLICATION : DEV\nDESCRIPTION : Profile: INVOCATION UNHANDLED EXCEPTION [null]\nDESCRIPTION : Profile: [ServiceAttribute]\nDESCRIPTION : Profile: Instance ID = 20130108124231841\n\r\n\r\n";
console.log(s.trim());

"ERROR: Profile : NOT SUCCESS
CODE        : 2
CATEGORY    : TECHNICAL
SEVERITY    : null
ENVIRONMENT : DEV
APPLICATION : DEV
DESCRIPTION : Profile: INVOCATION UNHANDLED EXCEPTION [null]
DESCRIPTION : Profile: [ServiceAttribute]
DESCRIPTION : Profile: Instance ID = 20130108124231841"

If trim()'s not available (IE 8-), try this polyfill:

if(!String.prototype.trim) {
    String.prototype.trim = function () { 
        return this.replace(/^[\s\uFEFF]+|[\s\uFEFF]+$/g,'');
    });
}
Cerbrus
  • 70,800
  • 18
  • 132
  • 147
  • trim doesn't work in all browsers (for ex, ie), so i recommend to use the [jQuery trim](http://api.jquery.com/jQuery.trim/) – lante Jan 08 '13 at 11:55
  • 1
    @lante It doesn't make sense to recommend jQuery just for that! – bfavaretto Jan 08 '13 at 11:57
  • @Cerbrus According to MDN, `String.trim` uses exactly the same `replace` parameters as in OP's code. So, why would the result be different? https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/String/Trim – Rikonator Jan 08 '13 at 11:58
  • @lante: Added an alternative. – Cerbrus Jan 08 '13 at 12:02
  • 1
    @Rikonator: In that case, It's not the OP's code that's causing the problem. – Cerbrus Jan 08 '13 at 12:06
  • @Cerbrus Yup, OP's method works for me; http://jsfiddle.net/EYaZc/. I'd wager OP is using `document.write` or something to see the result... – Rikonator Jan 08 '13 at 12:07
  • @Rikonator: Yup, I see. Problem solved, I guess :P – Cerbrus Jan 08 '13 at 12:09
1

You can start with using native replace() twice (reformatted to see reg exp):

"\r\n\r\n\r\n\r\n\r\n\r\n\r\n\t\r\n\tERROR: Profile : NOT SUCCESS\nCODE        : 2\nCATEGORY    : TECHNICAL\nSEVERITY    : null\nENVIRONMENT : DEV\nAPPLICATION : DEV\nDESCRIPTION : Profile: INVOCATION UNHANDLED EXCEPTION [null]\nDESCRIPTION : Profile: [ServiceAttribute]\nDESCRIPTION : Profile: Instance ID = 20130108124231841\n\r\n\r\n"
     .replace(/^\s+/, "")
     .replace(/\s+$/, "")

gives:

ERROR: Profile : NOT SUCCESS
CODE        : 2
CATEGORY    : TECHNICAL
SEVERITY    : null
ENVIRONMENT : DEV
APPLICATION : DEV
DESCRIPTION : Profile: INVOCATION UNHANDLED EXCEPTION [null]
DESCRIPTION : Profile: [ServiceAttribute]
DESCRIPTION : Profile: Instance ID = 20130108124231841
Grzegorz Gierlik
  • 11,112
  • 4
  • 47
  • 55
  • I am still getting same message. it should keep the \n which comes in-between text or it should replace \n with proper enter(next line) in view – RaceBase Jan 08 '13 at 12:00
  • I use Windows 7 and I tried that in W7 on Google Chrome console. On other systems end of line character can be different so different reg exp can be needed. I would start with this. What message do you get? – Grzegorz Gierlik Jan 08 '13 at 14:54
1

For some odd reason I don't really like regular expressions. Whenever possible I try to find other ways. For those sharing my opinion, here is pure JavaScript code to custom trim a string based on your list of characters:

function MyTrim(text) {
    //turn into a string in case it's other type:
    var result = text + "";

    //trim leading characters:
    while (result.length > 0 && IsWhiteSpace(result[0]))
        result = result.substr(1, result.length - 1);

    //trim trailing characters:
    while (result.length > 0 && IsWhiteSpace(result[result.length - 1]))
        result = result.substr(0, result.length - 1);

    return result;
}

function IsWhiteSpace(c) {
    return c == " " || c == "\r" || c == "\n" || c == "\t";
}

In your case:

var text = MyTrim(originalRequest.responseText);

Live test case.

Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208