[\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.
Pressing the backspace key on a computer terminal would generate the ASCII code 08, BS or Backspace, a control code which would delete the preceding character. That control code could also be accessed by pressing Control-H, as H is the eighth letter of the Latin alphabet.
Noun. 1. backspace character - a control character that indicates moving a space to the left. ASCII control character, control character - ASCII characters to indicate carriage return or tab or backspace; typed by depressing a key and the control key at the same time. Based on WordNet 3.0, Farlex clipart collection.
ASCII backspace ( BS ) removes previous character. print “ab” + “\b” + “c”
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:
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With