通配符:.

通配符匹配除行终止符之外的所有字符。如果设置了 s 标志,它也匹配行终止符。

¥A wildcard matches all characters except line terminators. It also matches line terminators if the s flag is set.

语法

¥Syntax

regex
.

描述

¥Description

. 匹配除 行终止符 之外的任何字符。如果设置了 s 标志,则 . 也匹配行终止符。

¥. matches any character except line terminators. If the s flag is set, . also matches line terminators.

. 匹配的确切字符集取决于正则表达式是否为 Unicode 感知。如果它支持 Unicode,则 . 匹配任何 Unicode 代码点;否则,它匹配任何 UTF-16 代码单元。例如:

¥The exact character set matched by . depends on whether the regex is Unicode-aware. If it is Unicode-aware, . matches any Unicode code point; otherwise, it matches any UTF-16 code unit. For example:

js
/../.test("😄"); // true; matches two UTF-16 code units as a surrogate pair
/../u.test("😄"); // false; input only has one Unicode character

示例

¥Examples

与量词一起使用

¥Usage with quantifiers

通配符通常与 quantifiers 一起使用来匹配任何字符序列,直到找到下一个感兴趣的字符。例如,以下示例以 # Title 的形式提取 Markdown 页面的标题:

¥Wildcards are often used with quantifiers to match any character sequence, until the next character of interest is found. For example, the following example extracts the title of a Markdown page in the form # Title:

js
function parseTitle(entry) {
  // Use multiline mode because the title may not be at the start of
  // the file. Note that the m flag does not make . match line
  // terminators, so the title must be on a single line
  // Return text matched by the first capturing group.
  return /^#[ \t]+(.+)$/m.exec(entry)?.[1];
}

parseTitle("# Hello world"); // "Hello world"
parseTitle("## Subsection"); // undefined
parseTitle(`
---
slug: Web/JavaScript/Reference/Regular_expressions/Wildcard
---

# Wildcard: .

A **wildcard** matches all characters except line terminators.
`); // "Wildcard: ."

匹配代码块内容

¥Matching code block content

以下示例匹配 Markdown 中由三个反引号括起来的代码块的内容。它使用 s 标志来使 . 匹配行终止符,因为代码块的内容可能跨越多行:

¥The following example matches the content of a code block enclosed by three backticks in Markdown. It uses the s flag to make . match line terminators, because the content of a code block may span multiple lines:

js
function parseCodeBlock(entry) {
  return /^```.*?^(.+?)\n```/ms.exec(entry)?.[1];
}

parseCodeBlock(`
\`\`\`js
console.log("Hello world");
\`\`\`
`); // "console.log("Hello world");"

parseCodeBlock(`
A \`try...catch\` statement must have the blocks enclosed in curly braces.

\`\`\`js example-bad
try
  doSomething();
catch (e)
  console.log(e);
\`\`\`
`); // "try\n  doSomething();\ncatch (e)\n  console.log(e);"

警告:这些示例仅供演示之用。如果你想解析 Markdown,请使用专用的 Markdown 解析器,因为有很多边缘情况需要考虑。

¥Warning: These examples are for demonstration only. If you want to parse Markdown, use a dedicated Markdown parser because there are many edge cases to consider.

规范

Specification
ECMAScript Language Specification
# prod-Atom

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看