迹忆客 专注技术分享

当前位置:主页 > 学无止境 > WEB前端 > JavaScript >

JavaScript 中仅在字符第一次出现时拆分字符串

作者:迹忆客 最近更新:2023/02/20 浏览次数:

String.split() 方法与数组解构结合使用,仅在字符第一次出现时拆分字符串。

数组解构语法将字符串的两个部分捕获在单独的变量中。

const str = 'fql-jiyik-com';
const [first, ...rest] = str.split('-');

console.log(first); // 👉️ fql
console.log(rest); // 👉️ ['jiyik', 'com']

const remainder = rest.join('-');
console.log(remainder); // 👉️ jiyik-com

JavaScript 中仅在字符第一次出现时拆分字符串

我们使用 String.split 方法在连字符 - 上拆分字符串。

我们使用数组解构赋值将数组的第一个元素分配给第一个变量。

rest ... 运算符用于将剩余的数组元素收集到 rest 变量中。

最后一步是使用 Array.join 方法将剩余数组与连字符分隔符连接起来。

如果我们必须经常只拆分第一次出现的字符串,请定义一个可重用的函数。

function splitFirstOccurrence(str, separator) {
  const [first, ...rest] = str.split(separator);

  const remainder = rest.join('-');

  return {first, remainder};
}

const result = splitFirstOccurrence('fql-jiyik-com', '-');
console.log(result); // { first: 'fql', remainder: 'jiyik-com' }

console.log(result.first); // fql
console.log(result.remainder); // jiyik-com

该函数将字符串和分隔符作为参数,并仅在第一次出现分隔符时拆分字符串。


使用限制仅在字符第一次出现时拆分字符串

如果拆分后只需要获取第一部分,则将 limit 参数设置为 1。

const arr = 'fql-jiyik-com'.split('-', 1);

console.log(arr); // 👉️ [ 'fql' ]

console.log(arr[0]); // 👉️ fql

String.split 方法采用分隔符,并在每次出现所提供的分隔符时将字符串拆分为一个数组。

String.split() 方法采用以下 2 个参数:

  • separator 描述每个拆分应该发生在哪里的模式。
  • limit 一个整数,用于指定要包含在数组中的子字符串数的限制。

当 limit 参数设置为 1 时,数组中只包含第一次出现分隔符之前的部分。

或者,我们可以使用 slice 方法。


使用 Array.slice() 仅在字符第一次出现时拆分字符串

要仅在字符第一次出现时拆分 JavaScript 字符串,请对字符串调用 slice() 方法,将字符的索引 + 1 作为参数传递给它。

slice 方法将返回字符第一次出现后的字符串部分。

const str = 'fql-jiyik-com';

const after = str.slice(str.indexOf('-') + 1);
console.log(after); // 👉️ jiyik-com

const before = str.slice(0, str.indexOf('-'));
console.log(before); // 👉️ fql

String.slice 方法提取字符串的一部分并将其返回,而不修改原始字符串。

String.slice() 方法采用以下参数:

  • start 索引 要包含在返回的子字符串中的第一个字符的索引
  • end 索引 要从返回的子字符串中排除的第一个字符的索引

当只有一个参数被传递给 String.slice() 方法时,切片会到达字符串的末尾。

第一个示例显示了如何获取字符最后一次出现之后的部分。

const str = 'fql-jiyik-com';

const after = str.slice(str.indexOf('-') + 1);
console.log(after); // 👉️ jiyik-com

我们想排除指定的字符,所以我们在其索引中添加 1 以立即开始。

第二个示例显示如何获取字符第一次出现之前的部分。

const str = 'fql-jiyik-com';

const before = str.slice(0, str.indexOf('-'));
console.log(before); // 👉️ fql

切片从索引 0 开始,一直到但不包括分隔符的索引。


使用正则表达式仅在字符第一次出现时拆分字符串

我们还可以通过将正则表达式传递给 split(),在字符第一次出现时拆分字符串。

const str = 'fql-jiyik-com';

const arr = str.split(/-(.*)/);
console.log(arr); // 👉️ [ 'fql', 'jiyik-com', '' ]

const [before, after] = arr;

console.log(before); // 👉️ fql
console.log(after); // 👉️ jiyik-com

正斜杠 // 标记正则表达式的开始和结束。

正则表达式的形式为 /{char_to_split_on}(.*)/

括号称为捕获组。

. 是一个特殊字符,匹配任何单个字符。

星号 * 字符与前面的项目(任何字符)匹配 0 次或更多次。

当我们与捕获组拆分时,匹配的结果将返回到数组中。

我们可以使用解构赋值将前两个数组元素分配给变量或访问特定索引处的数组。

const str = 'fql-jiyik-com';

const arr = str.split(/-(.*)/);
console.log(arr); // 👉️ fql

console.log(arr[0]); // 👉️ fql
console.log(arr[1]); // 👉️ jiyik-com

JavaScript 索引是从零开始的,因此数组中的第一个元素的索引为 0,最后一个元素的索引为 array.length - 1

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

Do you understand JavaScript closures?

发布时间:2025/02/21 浏览次数:108 分类:JavaScript

The function of a closure can be inferred from its name, suggesting that it is related to the concept of scope. A closure itself is a core concept in JavaScript, and being a core concept, it is naturally also a difficult one.

Do you know about the hidden traps in variables in JavaScript?

发布时间:2025/02/21 浏览次数:178 分类:JavaScript

Whether you're just starting to learn JavaScript or have been using it for a long time, I believe you'll encounter some traps related to JavaScript variable scope. The goal is to identify these traps before you fall into them, in order to av

How much do you know about the Prototype Chain?

发布时间:2025/02/21 浏览次数:150 分类:JavaScript

The prototype chain can be considered one of the core features of JavaScript, and certainly one of its more challenging aspects. If you've learned other object-oriented programming languages, you may find it somewhat confusing when you start

JavaScript POST

发布时间:2024/03/23 浏览次数:96 分类:JavaScript

本教程讲解如何在不使用 JavaScript 表单的情况下发送 POST 数据。

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便