输入边界断言:^, $
输入边界断言检查字符串中的当前位置是否是输入边界。输入边界是字符串的开头或结尾;或者,如果设置了 m
标志,则为行的开头或结尾。
¥An input boundary assertion checks if the current position in the string is an input boundary. An input boundary is the start or end of the string; or, if the m
flag is set, the start or end of a line.
语法
描述
¥Description
^
断言当前位置是输入的开始。$
断言当前位置是输入的结尾。两者都是断言,因此它们不消耗任何字符。
¥^
asserts that the current position is the start of input. $
asserts that the current position is the end of input. Both are assertions, so they don't consume any characters.
更准确地说,^
断言左侧的字符超出了字符串的范围;$
断言右侧的字符超出了字符串的范围。如果设置了 m
标志,则如果左侧的字符是 行终止符 字符,则 ^
也会匹配;如果右侧的字符是行终止符,则 $
也会匹配。
¥More precisely, ^
asserts that the character to the left is out of bounds of the string; $
asserts that the character to the right is out of bounds of the string. If the m
flag is set, ^
also matches if the character to the left is a line terminator character, and $
also matches if the character to the right is a line terminator.
除非设置了 m
标志,否则 ^
和 $
断言仅在放置在模式边界时才有意义,因为它们左侧或右侧的任何其他字符必然会导致断言失败。
¥Unless the m
flag is set, the ^
and $
assertions only make sense when placed at the boundaries of the pattern, because any other characters to the left or right of them would necessarily cause the assertion to fail.
y
标志不会改变这些断言的含义 - 另请参见 固定的粘性标志。
¥The y
flag doesn't change the meaning of these assertions — see also anchored sticky flag.
示例
删除尾部斜杠
¥Removing trailing slashes
以下示例从 URL 字符串中删除尾部斜杠:
¥The following example removes trailing slashes from a URL string:
function removeTrailingSlash(url) {
return url.replace(/\/$/, "");
}
removeTrailingSlash("https://example.com/"); // "https://example.com"
removeTrailingSlash("https://example.com/docs/"); // "https://example.com/docs"
匹配文件扩展名
¥Matching file extensions
以下示例通过匹配文件扩展名(始终位于字符串末尾)来检查文件类型:
¥The following example checks file types by matching the file extension, which always comes at the end of the string:
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
匹配整个输入
¥Matching entire input
有时你想确保正则表达式匹配整个输入,而不仅仅是输入的子字符串。例如,如果你要确定字符串是否是有效的 identifier,则可以将输入边界断言添加到模式的两端:
¥Sometimes you want to make sure that your regex matches the entire input, not just a substring of the input. For example, if you are determining if a string is a valid identifier, you can add input boundary assertions to both ends of the pattern:
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
该函数在进行 codegen(使用代码生成代码)时很有用,因为你可以使用与其他字符串属性不同的有效标识符,例如 点符号 而不是 括号表示法:
¥This function is useful when doing codegen (generating code using code), because you can use valid identifiers differently from other string properties, such as dot notation instead of bracket notation:
const variables = ["foo", "foo:bar", " foo "];
function toAssignment(key) {
if (isValidIdentifier(key)) {
return `globalThis.${key} = undefined;`;
}
// JSON.stringify() escapes quotes and other special characters
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;
规范
Specification |
---|
ECMAScript Language Specification # prod-Assertion |
浏览器兼容性
BCD tables only load in the browser
也可以看看
¥See also