48

[\b] matches a backspace character apparently. I can't understand how a string could contain a backspace character. Can someone give me a concrete example of how this would be used? Thanks so much.

phw
  • 213
  • 1
  • 12
1252748
  • 14,597
  • 32
  • 109
  • 229
  • Of course a string can contain a backspace “character” - in quotes here, because _control code_ would be more precise. But it’s part of ASCII (and therefor UTF-8 as well), so of course it can occur anywhere those encodings are used. – CBroe Jul 03 '13 at 01:06
  • 2
    What the environment this string is _used_ in will make of it – well that’s another question. Put into HTML or alerted/logged from JS it will most likely have no visible effect at all – but for example a really old (needle) printer that this string was send to could revert one character position, and print the following character over it … – CBroe Jul 03 '13 at 01:07
  • Yeah, saw your answer already. Thanks for clarification. – CBroe Jul 03 '13 at 01:08

3 Answers3

50

While all the others are right in general (that \b is a word boundary), \b does mean backspace inside character classes:

[\b]

This will indeed match a backspace character. It's just an ASCII control character that can appear in text (ASCII code 8, or 10 in octal). I suppose it's mostly used for legacy reasons, to add diacritical marks (e.g. you could do a, backspace, ´, to get á). Today these have been mostly replaced by Unicode combining marks.

How a string containing a backspace will ultimately look depends on the software that renders it. Consoles will probably still move the cursor back and overwrite what was there if the backspace is followed by new other characters. To see this, fire up an interactive scripting console (like node.js if you want to try it in JavaScript), and run

> console.log("abc\b\bdef")
adef

Note that, had you omitted def, you'd just get abc, because the backspace itself does not erase anything. It only moves the cursor back.

On the other hand, your browser might just ignore it in an input field. For instance, check out a Unicode converter, enter the JavaScript input abc\b\bdef in the bottom left input, hit "convert", and the "Characters" output will not have the bc erased.

By the way \b being a backspace in character classes is not unique to JavaScript, but interpreted this way in most regex flavors.

Further reading:

Community
  • 1
  • 1
Martin Ender
  • 43,427
  • 11
  • 90
  • 130
13

Within the context of a regular expression (outside of a character class) \b does not mean backspace; it means 'word boundary'. There are lots of uses for it. For example,

/\bword\b/

will match some word but not someword.

As m.butter points out you can use it to match a backspace character, if you place it within a character class. For example:

var input = "this is a \btest.";
/[\b]/.test(input); // true
p.s.w.g
  • 146,324
  • 30
  • 291
  • 331
  • @m.buettner: Yes, that's the only case. A backspace char is `\u0008`. – elclanrs Jul 03 '13 at 01:00
  • 1
    @elclanrs I'm sorry, I mistyped: question has been edited. I'm new to this. I meant [\b], backspace character. What would be a practical example of that. Thanks! – 1252748 Jul 03 '13 at 01:00
  • let's say `var input` is coming to us via a ``. what would the text (with that backspace) look like in the input box? Thanks – 1252748 Jul 03 '13 at 01:07
  • @thomas something like `` I suppose – p.s.w.g Jul 03 '13 at 01:08
  • @thomas When I inspect the result in chrome's developer tools, I see ``. I believe there is indeed a `\b` character in there, but perhaps it's not being displayed correctly. – p.s.w.g Jul 03 '13 at 01:15
2

Real-world example: we pass \b between clients (via a hosted server) using our own protocol to allow the users to backspace over text they already sent. In one type of client (this one is web-browser based but the others are not), I check for for backspace-key hit (e.which==8 // Ref. https://api.jquery.com/keydown/ ) and send '\b'; the other clients handle this (possibly automatically). When my web client receives \b then my code has to handle it appropriately.

Zeek2
  • 386
  • 4
  • 8