Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the use of the [\b] backspace regex?

[\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.

like image 301
1252748 Avatar asked Jul 03 '13 00:07

1252748


People also ask

What does ASCII backspace do?

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.

What is the backspace character?

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.

What is ASCII backspace in Python?

ASCII backspace ( BS ) removes previous character. print “ab” + “\b” + “c”


3 Answers

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:

  • The "backspace" escape character '\b' in C, unexpected behavior?
  • Using the backspace character to delete a character on the console
  • Good ol' Wikipedia
like image 123
Martin Ender Avatar answered Sep 21 '22 14:09

Martin Ender


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 
like image 39
p.s.w.g Avatar answered Sep 17 '22 14:09

p.s.w.g


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.

like image 39
Zeek2 Avatar answered Sep 21 '22 14:09

Zeek2