字符类 - JavaScript 编辑

字符类可以区分各种字符,例如区分字母和数字。

类型

The following table is also duplicated on this cheatsheet. Do not forget to edit it as well, thanks!
CharactersMeaning
.

有下列含义之一:

  • 匹配除行终止符之外的任何单个字符: \n, \r, \u2028 or \u2029. 例如, /.y/ 在“yes make my day”中匹配“my”和“ay”,而不是“yes”。
  • 在字符集内,点失去了它的特殊意义,并与文字点匹配。

需要注意的是,m multiline标志不会改变点的行为。因此,要跨多行匹配一个模式,可以使用字符集[^]—它将匹配任何字符,包括新行。

ES2018 添加了 s "dotAll" 标志,它允许点也匹配行终止符。

\d

匹配任何数字(阿拉伯数字)。 相当于 [0-9]. 例如, /\d/ 或 /[0-9]/ 匹配 “B2is the suite number”中的“2”。

\D

匹配任何非数字(阿拉伯数字)的字符。相当于[^0-9]. 例如, /\D/ or /[^0-9]/ 在 "B2 is the suite number" 中 匹配 "B".

\w

匹配基本拉丁字母中的任何字母数字字符,包括下划线。相当于 [A-Za-z0-9_]. 例如, /\w/ 在 "apple" 匹配 "a" , "5" in "$5.28", "3" in "3D" and "m" in "Émanuel".

\W

匹配任何不是来自基本拉丁字母的单词字符。相当于 [^A-Za-z0-9_]. 例如, /\W/ or /[^A-Za-z0-9_]/ 匹配 "%" 在 "50%" 以及 "É" 在 "Émanuel" 中.

\s

Matches a single white space character, including space, tab, form feed, line feed, and other Unicode spaces. Equivalent to [ \f\n\r\t\v\u00a0\u1680\u2000-\u200a\u2028\u2029\u202f\u205f\u3000\ufeff]. For example, /\s\w*/ matches " bar" in "foo bar".

\S

Matches a single character other than white space. Equivalent to [^ \f\n\r\t\v\u00a0\u1680\u2000-\u200a\u2028\u2029\u202f\u205f\u3000\ufeff]. For example, /\S\w*/ matches "foo" in "foo bar".

\tMatches a horizontal tab.
\rMatches a carriage return.
\nMatches a linefeed.
\vMatches a vertical tab.
\fMatches a form-feed.
[\b]Matches a backspace. If you're looking for the word-boundary character (\b), see Boundaries.
\0Matches a NUL character. Do not follow this with another digit.
\cX

Matches a control character using caret notation, where "X" is a letter from A–Z (corresponding to codepoints U+0001U+001F). For example, /\cM/ matches "\r" in "\r\n".

\xhhMatches the character with the code hh (two hexadecimal digits).
\uhhhhMatches a UTF-16 code-unit with the value hhhh (four hexadecimal digits).
\u{hhhh} or \u{hhhhh}(Only when the u flag is set.) Matches the character with the Unicode value U+hhhh or U+hhhhh (hexadecimal digits).
\

Indicates that the following character should be treated specially, or "escaped". It behaves one of two ways.

  • For characters that are usually treated literally, indicates that the next character is special and not to be interpreted literally. For example, /b/ matches the character "b". By placing a backslash in front of "b", that is by using /\b/, the character becomes special to mean match a word boundary.
  • For characters that are usually treated specially, indicates that the next character is not special and should be interpreted literally. For example, "*" is a special character that means 0 or more occurrences of the preceding character should be matched; for example, /a*/ means match 0 or more "a"s. To match * literally, precede it with a backslash; for example, /a\*/ matches "a*".

To match this character literally, escape it with itself. In other words to search for \ use /\\/.

Examples

Looking for a series of digits

var randomData = "015 354 8787 687351 3512 8735";
var regexpFourDigits = /\b\d{4}\b/g;
// \b indicates a boundary (i.e. do not start matching in the middle of a word)
// \d{4} indicates a digit, four times
// \b indicates another boundary (i.e. do not end matching in the middle of a word)


console.table(randomData.match(regexpFourDigits));
// ['8787', '3512', '8735']

Looking for a word (from the latin alphabet) starting with A

var aliceExcerpt = "I’m sure I’m not Ada,’ she said, ‘for her hair goes in such long ringlets, and mine doesn’t go in ringlets at all.";
var regexpWordStartingWithA = /\b[aA]\w+/g;
// \b indicates a boundary (i.e. do not start matching in the middle of a word)
// [aA] indicates the letter a or A
// \w+ indicates any character *from the latin alphabet*, multiple times

console.table(aliceExcerpt.match(regexpWordStartingWithA));
// ['Ada', 'and', 'at', 'all']

Looking for a word (from Unicode characters)

Instead of the Latin alphabet, we can use a range of Unicode characters to identify a word (thus being able to deal with text in other languages like Russian or Arabic). The "Basic Multilingual Plane" of Unicode contains most of the characters used around the world and we can use character classes and ranges to match words written with those characters.

var nonEnglishText = "Приключения Алисы в Стране чудес";
var regexpBMPWord = /([\u0000-\u0019\u0021-\uFFFF])+/gu;
// BMP goes through U+0000 to U+FFFF but space is U+0020

console.table(nonEnglishText.match(regexpBMPWord));
[ 'Приключения', 'Алисы', 'в', 'Стране', 'чудес' ]

Note for MDN editors: please do not try to add funny examples with emoji as those characters are not handled by the platform (Kuma).

Specifications

Specification
ECMAScript (ECMA-262)
RegExp: Character classes

Browser compatibility

For browser compatibility information, check out the main Regular Expressions compatibility table.

See also

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:89 次

字数:10636

最后编辑:7年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文