发布于 2026-01-06 0 阅读
0

JavaScript 中的字符串 endsWith() 方法

JavaScript 中的字符串 endsWith() 方法

CodeTidbit 由 SamanthaMing.com 提供

想知道一个字符串是否以某个字符结尾?很简单,使用 ES6 的 `endsWith` 方法。即使你不是开发人员,也能轻松理解它的含义。让语言更易于阅读绝对是明智之举 💪

const workout = '🏃 🏋️ 💪';

// Old Way
workout.indexOf('💪', workout.length - '💪'.length) !== -1;
// true

// ✅ES6 Way
workout.endsWith('💪');
// true
Enter fullscreen mode Exit fullscreen mode

参数

endsWith方法接受 2 个参数:

  1. 搜索值
  2. 长度

1. 搜索值

这是必填字段。请在此处输入搜索值。搜索值可以是单个字符,也可以是多个字符。我们来看一些示例。

单字符

const name = 'Samantha Ming';

name.endsWith('g'); // true
name.endsWith('n'); // false
Enter fullscreen mode Exit fullscreen mode

多个角色

const name = 'Samantha Ming';

name.endsWith('ing'); // true
name.endsWith('in'); // false
Enter fullscreen mode Exit fullscreen mode

多个词

const name = 'Samantha Ming';

name.endsWith('tha Ming'); // true
name.endsWith('tha M'); // false
Enter fullscreen mode Exit fullscreen mode

2. 长度

在这里你可以指定要搜索的字符串长度。我第一次看到这一点时,也感到很困惑。所以,我们来试着理解一下长度在这里的作用。

首先,我想告诉你我名字的长度。

console.log('Samantha'.length); // 8
// The last character is 'a'
Enter fullscreen mode Exit fullscreen mode

现在让我们来利用长度参数。

const name = 'Samantha Ming';

name.endsWith('a', 8); // true
Enter fullscreen mode Exit fullscreen mode

☝️所以当我说“提取字符串的前8个字符(从左到右)”时8,我是在告诉程序提取字符串的前8个字符。在这个例子中,就是字符串的前8个字符。而这8个字符就是我们要搜索的字符串endsWithnameendsWith

上述示例与执行以下操作类似:

const name = 'Samantha';

name.endsWith('a'); // true
Enter fullscreen mode Exit fullscreen mode

我一开始感到困惑的原因是,我把之前对 `\t` 的理解套用到了 `\t`startsWith中,其中第二个参数是起始索引。所以我以为 `\t` 的第二个参数也endsWith遵循同样的模式,是反向的结束索引😅。这就是为什么人们常说,永远不要“想当然”。当你以为自己懂的时候,其实你并不懂。所以要保持谦逊,永远保持学习的心态🤓

如果你感兴趣,这是我的代码注释startsWith

代码注释:startsWith

区分大小写

这里介绍的知识点与此类似startsWith。该endsWith方法也区分大小写。

const name = 'Samantha Ming';

name.endsWith('G'); // false
name.endsWith('g'); // true
Enter fullscreen mode Exit fullscreen mode

浏览器支持

浏览器支持真是太棒了!当然,如果你不把 Internet Explorer 算在内的话😅(抱歉各位,但我相信这并不令人意外😂)

浏览器支持:以...结尾

但没关系,这里有 polyfill,所以你可以安全地使用它endsWith,并且它仍然会在 IE 中得到支持。

MDN Polyfill:以...结尾

顺便一提,如果你对 Polyfill 是什么还不太了解,这里有一个 Tyler McGinnis 制作的非常棒的视频。

使用 Babel 编译与 Polyfill(JavaScript)

社区意见

💬 你还知道哪些其他古老的方法?

使用正则表达式

@maxstalker我建议采用旧方法,使用一些柯里化的辅助函数,并辅以一些正则表达式。

const startsWith = pattern => string =>
  new RegExp(`^${pattern}.*`).test(string);

const endsWith = pattern => string => new RegExp(`.*${pattern}$`).test(string);

const sports = "🏈🎳⛳⛸";
console.log(startsWith("🏈")(sports)); // true
console.log(startsWith("")(sports)); // false

console.log(endsWith("🏈")(sports)); // false
console.log(endsWith("")(sports)); // true
Enter fullscreen mode Exit fullscreen mode

@maxstalker或者稍短一些的版本,您可以根据需要随时插入:

const sports = "🏈🎳⛳⛸";

// startsWith
console.log(/^🎳.*/.test(sports)); // false
console.log(/^🏈.*/.test(sports)); // true

// endsWith
console.log(/.*🎳$/.test(sports)); // false
console.log(/.*⛸$/.test(sports)); // true
Enter fullscreen mode Exit fullscreen mode

使用slice

@maxstalker另一种方法——虽然有点复杂,也可能更偏理论——是比较字符串的各个部分:

const sports = "⛳🎳🏈⚽🎾";
const startPattern = "";
const endPattern = "⚽🎾";

//startsWith
console.log(sports.slice(0, startPattern.length) === "wrong!"); // false
console.log(sports.slice(0, startPattern.length) === startPattern); // true

// endsWith
console.log(sports.slice(-endPattern.length) === "wrong!"); // false
console.log(sports.slice(-endPattern.length) === endPattern); // true
Enter fullscreen mode Exit fullscreen mode

使用lastIndexOf

const workout = '🏃 🏋️ 💪';

workout.lastIndexOf('💪') === workout.length - '💪'.length;
// true
Enter fullscreen mode Exit fullscreen mode

资源


感谢阅读❤
欢迎关注!Instagram | Twitter | Facebook |博客| SamanthaMing.com

文章来源:https://dev.to/samanthaming/string-endswith-method-in-javascript-o98