views:

96

answers:

2

I was looking for a trim function in JavaScript which doesn't exist and some code on Goggling suggests that use:

function trimStr(str) {
  return str.replace(/^\s+|\s+$/g, '');
}

I want to know how str.replace(/^\s+|\s+$/g, '') works. I understand that this is some form of regular expression but dont know what it is doing.

+4  A: 

^ is the beginning of the string, and $ is the end. \s means a whitespace character (which in JavaScript specifically means tab, vertical tab, form feed, space, non-break space, byte order mark, Unicode space separator (category Zs), line feed, carriage return, line separator, or paragraph separator), and + means 1 or more. | is alternation, a choice between two possibilities. g is the global flag. So the regex means the beginning, then one or more whitespace, or one or more whitespace, then the end. Then, we replace all matches (since it's global) with the empty string.

You might be interested in this blog post, which analyzes in more detail than you probably need :) the pros and cons of various trim functions.

Matthew Flaschen
`\s` means more than a space (it means whitespace). In JS it includes the characters `[ \r\n\t]`.
Andy E
@Andy, thanks, I was actually aware of that, but didn't phrase it well. I didn't mean to say it was just ASCII 0x20 (actual space). I've clarified; it's actually several more characters than you said.
Matthew Flaschen
@Matt: well done for the further clarification, I just included the characters I was completely sure of. You should know, though, that `\s` is only specified to match the BOM as of ECMA-262 5th edition (it wasn't included in the 3rd edition). Also, in Internet Explorer up to version 8, the exact characters matched by `\s` is equivalent to `[ \f\n\r\t\v]` - so your list isn't an ideal reference for full cross-browser compatibility.
Andy E
@Andy, yeah, I did notice BOM is marked in Annex E as an addition. Thanks for the note about IE. As browser bugs go, this is pretty minor. How many people enter non-breaking space or line separator into a text box? If it *is* important, the filtering should be done on the server side anyway.
Matthew Flaschen
@Matt: agreed. Stuff like that usually only happens during copy/paste.
Andy E
+7  A: 

/^\s+|\s+$/g searches for whitespace from either the beginning or the end of the string. The expression can be split into two parts, ^\s+ and \s+$ which are separated by | (OR). The first part starts from the beginning of the string (^) and includes as many whitespace characters it can (\s+). The second part does the same but in reverse and for the end using the dollar sign ($).

In plain english, the regular expression would go like this:

Find as many whitespace characters from the beginning of the string as possible or as many whitespace characters from the end as possible.

Note that \s matches spaces, tabs and line breaks.

The /g part at the end enables global searching, which allows multiple replacements (eg. not just the beginning, but the end of the string also).

Tatu Ulmanen
@Tatu got everything except /g. Could you give more explanation around that any more examples
sushil bharwani
@Tatu i am confused with "/g part at the end enables global searching" why do we need it here. As we have already included condition for begining and end of string
sushil bharwani
@sushil, here the global flag means that there can be more than one replacement. Without it, it would only replace the first match. For instance. `" foo ".replace(/^\s+|\s+$/, "")` (no global) is `"foo "`
Matthew Flaschen