正则表达式混淆 \s 和 "" [英] regular expression confusion \s and " "

查看:67
本文介绍了正则表达式混淆 \s 和 ""的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在正则表达式中,我知道什么时候使用 \s 来表示一个空格,但是,在以下情况下,它们会有所不同:

In regular expression, i know when use \s to represent a space, but, in following case, would they be different:

  1. /a\sb/---带有 \s
  2. /a b/---有空字段

如果你能向我解释,非常感谢.

thanks a lot if you can explain to me.

推荐答案

\s 字符类匹配所有空白字符",而不仅仅是空格.这包括制表符 (\t),如果允许多行匹配,则包括回车 (\r) 和换行符 (\n).从理论上讲,如果您的正则表达式引擎处理 unicode,那么 \s 也可以匹配 unicode 空白字符,但您的里程可能会有所不同.

The \s character class matches all "whitespace characters," not just spaces. This includes tabs (\t), and if multiline matching is allowed, it includes carriage return (\r) and newline (\n). Theoretically, if your regular expression engine handles unicode, there are also unicode whitespace characters that \s can match, though your mileage may vary.

因此,对于像 "a\t b" 这样的字符串,您可以将它与正则表达式/a\s+b/匹配,以防万一这对您有用.

So with a string like "a\t b", you can match it with the regex /a\s+b/, in case that is useful to you.

这篇关于正则表达式混淆 \s 和 ""的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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