Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@ console.log(text.match(regexpFoxQuality));
<td><code>^</code></td>
<td>
<p>
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

L6 which indicate the beginnings and endings of lines and words, and other patterns indicating in some way that a match is possible (including look-ahead, look-behind, and conditional expressions).

似乎前译者的翻译把两个部分混淆到一块了?

匹配输入的开头。如果多行模式设为 true,<code>^</code>
在换行符后也能立即匹配,比如 <code>/^A/</code> 匹配不了 "an A" 里面的
"A",但是可以匹配 "An A" 里面第一个 "A"。
<a href="/zh-CN/docs/Web/JavaScript/Reference/Regular_expressions/Input_boundary_assertion"><strong>起始输入边界断言</strong></a>:匹配输入的起始。如果启用了 <a href="/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/RegExp/multiline"><code>multiline</code></a>(m)标志,则也会匹配换行符之后的字符。比如 <code>/^A/</code> 不匹配“an A”中的“A”,但是可以匹配“An A”中第一个“A”。
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

btw,冒号是不是该被加粗?

<strong>Input boundary beginning assertion:</strong>

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

后面完整翻译的时候再统一改吧,都可以的。个人感觉比较好的方式还是把冒号放外面。

</p>
<div class="notecard note">
<p>
Expand All @@ -58,9 +56,7 @@ console.log(text.match(regexpFoxQuality));
<td><code>$</code></td>
<td>
<p>
匹配输入的结束。如果多行模式设为 true,<code>$</code>
在换行符前也能立即匹配,比如 <code>/t$/</code> 不能匹配 "eater" 中的
"t",但是可以匹配 "eat" 中的 "t"。
<a href="/zh-CN/docs/Web/JavaScript/Reference/Regular_expressions/Input_boundary_assertion"><strong>终止输入边界断言</strong></a>:匹配输入的结束。如果启用了 <a href="/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/RegExp/multiline"><code>multiline</code></a>(m)标志,则也会匹配换行符之后的字符。比如 <code>/t$/</code> 不匹配“eater”中的“t”,但是可以匹配“eat”中的“t”。
</p>
</td>
</tr>
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

L84

          若要匹配退格字符 (<code>[\b]</code>),参见<a href="/zh-CN/docs/Web/JavaScript/Guide/Regular_expressions/Character_classes">字符类</a>

格式问题

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

以及L69改成“示例”吧,和别的地方统一起来

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

Expand Down Expand Up @@ -195,6 +191,8 @@ console.log(text.match(regexpFoxQuality));

### 一般边界类型概述示例

<!-- cSpell:ignore greon -->

```js
// 使用正则表达式边界修复错误字符串
buggyMultiline = `tey, ihe light-greon apple
Expand All @@ -217,35 +215,38 @@ fixedMultiline = buggyMultiline.replace(/\Bo/gim, "e");
console.log(4, fixedMultiline); // 修复 'greon' => 'green',而不对 'on' 做改动。
```

### 使用 ^(控制字符)匹配输入的开头
### 使用控制字符 ^ 匹配输入的开头

使用 `^`匹配输入的开头。在这个例子中,我们可以通过 `/^A/` 正则表达式得到以 A 开头的水果。为了选择合适的水果,我们可以使用带有箭头函数的过滤方法
使用 `^` 匹配输入的开头。在这个例子中,我们可以通过 `/^A/` 正则表达式得到以 A 开头的水果。为了选择合适的水果,我们可以使用带有[箭头](/zh-CN/docs/Web/JavaScript/Reference/Functions/Arrow_functions)函数的 [`filter`](/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/filter) 方法

```js
const fruits = ["Apple", "Watermelon", "Orange", "Avocado", "Strawberry"];

// 使用正则 /^A/ 选择以'A'开头的水果。
// 这里的 '^' 只有一种含义:匹配输入的开头
// 使用正则 /^A/ 选择以‘A’开头的水果。
// 这里的‘^’只有一种含义:匹配输入的起始

const fruitsStartsWithA = fruits.filter((fruit) => /^A/.test(fruit));
console.log(fruitsStartsWithA); // [ 'Apple', 'Avocado' ]
```

在第二个示例中,`^` 既用于在输入开头进行匹配,也用于在[字符类](/zh-CN/docs/Web/JavaScript/Guide/Regular_expressions/Character_classes)中使用时创建字符类补集。
在第二个示例中,`^` 既用于在输入起始进行匹配,也用于在[字符类](/zh-CN/docs/Web/JavaScript/Guide/Regular_expressions/Character_classes)中使用时创建字符类补集。

```js
const fruits = ["Apple", "Watermelon", "Orange", "Avocado", "Strawberry"];

// 使用正则 /^[^A]/ 选择 不是以‘A’开头的水果
// 使用正则表达式 /^[^A]/ 选择不是以‘A’开头的水果
// 在这个例子中,“^”控制符号表示两种含义:
// 1) 匹配输入的开头
// 2) 一个否定的字符集:[^A],意思是匹配不是‘A’的字符
// 2) 一个否定的字符补集:[^A]
// 也就是说,它匹配所有未被方括号包围的内容。

const fruitsStartsWithNotA = fruits.filter((fruit) => /^[^A]/.test(fruit));

console.log(fruitsStartsWithNotA); // [ 'Watermelon', 'Orange', 'Strawberry' ]
```

在[输入边界断言](/zh-CN/docs/Web/JavaScript/Reference/Regular_expressions/Input_boundary_assertion)参考中查看更多示例。

### 匹配单词边界

在本示例中,我们匹配以“en”或“ed”结尾的水果名称。
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
---
title: 输入边界断言:^、$
slug: Web/JavaScript/Reference/Regular_expressions/Input_boundary_assertion
l10n:
sourceCommit: fad67be4431d8e6c2a89ac880735233aa76c41d4
---

**输入边界断言**检查字符串的当前位置是否为输入边界。输入边界为字符串的起始或终止,或者,如果设置了 `m` 标志,则为一行的起始或终止。

## 语法

```regex
^
$
```

## 描述

`^` 断言当前位置是输入的开头,`$` 断言当前位置是输入的结尾。因为两者都是*断言*,它们不会消耗任何字符。
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[mdn-linter] reported by reviewdog 🐶

Suggested change
`^` 断言当前位置是输入的开头,`$` 断言当前位置是输入的结尾。因为两者都是*断言*,它们不会消耗任何字符。
`^` 断言当前位置是输入的开头,`$` 断言当前位置是输入的结尾。因为两者都是*断言*,它们不会消耗任何字符。


更准确地说,`^` 断言左侧的字符超出了字符串的范围;`$` 断言右侧的字符超出了字符串的范围。如果设置了 [`m`](/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/RegExp/multiline) 标志,则 `^` 也会匹配左侧字符是[行终止符](/zh-CN/docs/Web/JavaScript/Reference/Lexical_grammar#行终止符)的情况,`$` 同理也会匹配右侧字符是行结束符的情况。
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[mdn-linter] reported by reviewdog 🐶

Suggested change
更准确地说,`^` 断言左侧的字符超出了字符串的范围;`$` 断言右侧的字符超出了字符串的范围。如果设置了 [`m`](/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/RegExp/multiline) 标志,则 `^` 也会匹配左侧字符是[行终止符](/zh-CN/docs/Web/JavaScript/Reference/Lexical_grammar#行终止符)的情况,`$` 同理也会匹配右侧字符是行结束符的情况。
更准确地说,`^` 断言左侧的字符超出了字符串的范围;`$` 断言右侧的字符超出了字符串的范围。如果设置了 [`m`](/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/RegExp/multiline) 标志,则 `^` 也会匹配左侧字符是[行终止符](/zh-CN/docs/Web/JavaScript/Reference/Lexical_grammar#行终止符)的情况,`$` 同理也会匹配右侧字符是行结束符的情况。


除非设置了 `m` 标志,否则 `^` 和 `$` 断言仅在位于模式边界时才有意义,因为它们左侧或右侧的任何其他字符都会导致断言失败。

`y` 标志不会改变这些断言的含义——参见[锚定粘性标志](/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/RegExp/sticky#锚定的_sticky_标志)。

## 示例

### 移除末尾的斜杠

以下示例用于从 URL 字符串中移除尾随斜杠:

```js
function removeTrailingSlash(url) {
return url.replace(/\/$/, "");
}

removeTrailingSlash("https://example.com/"); // "https://example.com"
removeTrailingSlash("https://example.com/docs/"); // "https://example.com/docs"
```

### 匹配文件扩展名

以下示例通过匹配文件扩展名来检查文件类型,文件扩展名总是位于字符串的末尾:

```js
function isImage(filename) {
return /\.(?:png|jpe?g|webp|avif|gif)$/i.test(filename);
}

isImage("image.png"); // true
isImage("image.jpg"); // true
isImage("image.pdf"); // false
```

### 匹配整个输入

有时,需要确保正则表达式匹配整个输入内容,而不仅仅是输入的子字符串。例如,如果需要判断一个字符串是否为有效的[标识符](/zh-CN/docs/Web/JavaScript/Reference/Lexical_grammar#标识符),可以在模式的两端添加输入边界断言:

```js
function isValidIdentifier(str) {
return /^[$_\p{ID_Start}][$_\p{ID_Continue}]*$/u.test(str);
}

isValidIdentifier("foo"); // true
isValidIdentifier("$1"); // true
isValidIdentifier("1foo"); // false
isValidIdentifier(" foo "); // false
```

该函数在进行代码生成(即使用代码生成代码)时非常有用,因为你可使用与其他字符串属性不同的合法标识符,如使用[点号表示法](/zh-CN/docs/Web/JavaScript/Reference/Operators/Property_accessors#点号表示法)代替[方括号表示法](/zh-CN/docs/Web/JavaScript/Reference/Operators/Property_accessors#方括号表示法):
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[mdn-linter] reported by reviewdog 🐶

Suggested change
该函数在进行代码生成(即使用代码生成代码)时非常有用,因为你可使用与其他字符串属性不同的合法标识符,如使用[点号表示法](/zh-CN/docs/Web/JavaScript/Reference/Operators/Property_accessors#点号表示法)代替[方括号表示法](/zh-CN/docs/Web/JavaScript/Reference/Operators/Property_accessors#方括号表示法)
该函数在进行代码生成(即使用代码生成代码)时非常有用,因为你可使用与其他字符串属性不同的合法标识符,如使用[点号表示法](/zh-CN/docs/Web/JavaScript/Reference/Operators/Property_accessors#点号表示法)代替[方括号表示法](/zh-CN/docs/Web/JavaScript/Reference/Operators/Property_accessors#方括号表示法)


```js
const variables = ["foo", "foo:bar", " foo "];

function toAssignment(key) {
if (isValidIdentifier(key)) {
return `globalThis.${key} = undefined;`;
}
// JSON.stringify() 转义引号和其他特殊字符
return `globalThis[${JSON.stringify(key)}] = undefined;`;
}

const statements = variables.map(toAssignment).join("\n");

console.log(statements);
// globalThis.foo = undefined;
// globalThis["foo:bar"] = undefined;
// globalThis[" foo "] = undefined;
```

## 规范

{{Specifications}}

## 浏览器兼容性

{{Compat}}

## 参见

- [断言](/zh-CN/docs/Web/JavaScript/Guide/Regular_expressions/Assertions)指南
- [正则表达式](/zh-CN/docs/Web/JavaScript/Reference/Regular_expressions)
- [单词边界断言:`\b`、`\B`](/zh-CN/docs/Web/JavaScript/Reference/Regular_expressions/Word_boundary_assertion)
- [前向断言:`(?=...)`、`(?!...)`](/zh-CN/docs/Web/JavaScript/Reference/Regular_expressions/Lookahead_assertion)
- [后向断言:`(?<=...)`、`(?<!...)`](/zh-CN/docs/Web/JavaScript/Reference/Regular_expressions/Lookbehind_assertion)
Loading