[\ b]退格正则表达式有什么用? [英] What's the use of the [\b] backspace regex?

查看:124
本文介绍了[\ b]退格正则表达式有什么用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

[\b]显然与退格字符匹配.我不明白字符串如何包含退格字符. 有人可以给我一个具体的例子来说明如何使用它吗?非常感谢.

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

推荐答案

虽然其他所有命令总体上都是正确的(\b是单词边界),但\b 是指内部的空格字符类:

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

[\b]

这确实会匹配一个退格字符.它只是一个ASCII控制字符,可以显示在文本中(ASCII码8,或八进制10).我想它主要是出于遗留原因,用于添加变音标记(例如,您可以执行a,退格键,´以获取á).如今,这些已被Unicode组合标记所取代.

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.

包含退格符的字符串最终如何看起来 取决于呈现它的软件.如果退格后跟其他新字符,则控制台可能仍会将光标移回并覆盖那里的内容.要看到这一点,请启动一个交互式脚本控制台(例如,如果要在JavaScript中尝试,请使用node.js),然后运行

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

请注意,如果您省略了def,则只会得到abc,因为退格键本身不会删除任何内容.只会将光标向后移动.

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.

另一方面,您的浏览器可能只是在输入字段中忽略了它.例如,检查 Unicode转换器,然后在左下方输入中输入JavaScript输入abc\b\bdef,点击转换",字符"输出将不会删除bc.

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.

\b在字符类中作为退格符并不是JavaScript独有的,而是在大多数正则表达式中都是这样解释的.

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

进一步阅读:

  • The "backspace" escape character '\b' in C, unexpected behavior?
  • Using the backspace character to delete a character on the console
  • Good ol' Wikipedia

这篇关于[\ b]退格正则表达式有什么用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆