tags:

views:

68

answers:

2
function trim(str,options){
  var   string = str.replace(/^\s\s*/, ''),
        ws = /\s/,
        i = str.length,
        j = -1;
  if(options==="begin"){while(ws.test(string.charAt(++j)));return string.slice(j,i);}
  if(options==="end"){while(ws.test(string.charAt(--i)));return string.slice(j+1,i+1);}
  while(ws.test(string.charAt(--i)));while(ws.test(string.charAt(++j)));
  return string.slice(j, i + 1);
}

This function is an implementation of the Trim() method that you find in C#.

I've added options for removing withspace both at the beginning and end. The problem is, I can't get it to work in a demo.

What I've done is this:

var a = "           zareaerar  arzare        ";
var b = "aezze          azeze      a    ";
var c = "azrazza rzrzrzrp"
var d = " aezzaeazeazeaz         azez ";

document.write('<p style="backround:#ff0000">',trim(a,"begin"),'</p>','<br />');
document.write('<p style="backround:#ff0000">',trim(b,"begin"),'</p>','<br />');
document.write('<p style="backround:#ff0000">',trim(c,"begin"),'</p>','<br />');
document.write('<p style="backround:#ff0000">',trim(d,"begin"),'</p>','<br />');

First of all, no background-color appears and the strings also seem to loose the spaces in the middle of the string...

Does this work with user-input only? Are strings automatically trimmed these days?

+8  A: 

1) You are not seeing a background since you have a typo: backround instead of background

2) In HTML there is no significance for more than one whitespace. You need to use &nbsp; if you want to insert a non-breaking whitespace between words in a paragraph.

Reference (W3 spec):

Note that a sequence of white spaces between words in the source document may result in an entirely different rendered inter-word spacing (except in the case of the PRE element). In particular, user agents should collapse input white space sequences when producing output inter-word space. This can and should be done even in the absence of language information (from the lang attribute, the HTTP "Content-Language" header field (see [RFC2616], section 14.12), user agent settings, etc.).

The PRE element is used for preformatted text, where white space is significant.

Yuval A
Yeah, you know what? I've never had that problem before I've never needed to use   so long as the words were between quotes...
baeltazor
I doubt that. Check out the W3 HTML spec...
Yuval A
@baeltazor: I seriously doubt that as well. Unless you are serving your context as "text/plain" there should be no *visible* difference between " " (two spaces) and " " (single space) in the output. Quoting is immaterial.
paracycle
+2  A: 

I know this is not the answer to your question but why is your trim function so complicated?

You can just do:

function trim(str, option) {
    if (option === "begin") return str.replace(/^\s+/,"");
    if (option === "end") return str.replace(/\s+$/,"");

    return str.replace(/^\s+|\s+$/g,"");
}

If you are using regular expression tests anyway, you might as well just replace all the whitespaces with the replace method.

Note: Code adapted from here.

paracycle