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 :(

2
  • If you have JQuery, you could use $.trim(). But dont include JQuery just for this... Commented Jan 8, 2013 at 11:51
  • 2
    What you have should not modify the new-lines within the string; jsfiddle.net/dmwpb make sure you view the output in something that honours new lines. Commented Jan 8, 2013 at 11:58

3 Answers 3

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,'');
    });
}
Sign up to request clarification or add additional context in comments.

7 Comments

trim doesn't work in all browsers (for ex, ie), so i recommend to use the jQuery trim
@lante It doesn't make sense to recommend jQuery just for that!
@Cerbrus According to MDN, String.trim uses exactly the same replace parameters as in OP's code. So, why would the result be different? developer.mozilla.org/en-US/docs/JavaScript/Reference/…
@lante: Added an alternative.
@Rikonator: In that case, It's not the OP's code that's causing the problem.
|
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

2 Comments

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
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?
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.

8 Comments

@Cerbrus trivial to add, yeah.. you happen to know any native JS method that identifies character as whitespace?
I don't think there's a String.isWhiteSpace function. A check for "\t" should do the trick, though :P
I called this way var text = MyTrim(originalRequest.responseText); but I am still getting the text as original text (atleast in IE debug) and in output page without enters. :(
@NitinGurram sorry, forgot to remove tab characters. Try now..
@NitinGurram You need the replace to be global. text.replace(/\n/g, "<br />") should do the trick. jsfiddle.net/EYaZc/1
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.