在我的代码中,我基于_分割字符串并获取数组中的第二项。

var element = $(this).attr('class');
var field = element.split('_')[1];

带来好运,给我带来好运。工作好了!

但是,现在我有了一个看起来像good_luck_buddy的类。我如何让我的javascript忽略第二个_,给我luck_buddy?

我找到了var field = element。Split (new char [] {'_'}, 2);在c# stackoverflow中回答,但它不起作用。我尝试在jsFiddle…


当前回答

这个解决方案对我很有效

var str = "good_luck_buddy";
var index = str.indexOf('_');
var arr = [str.slice(0, index), str.slice(index + 1)];

//arr[0] = "good"
//arr[1] = "luck_buddy"

OR

var str = "good_luck_buddy";
var index = str.indexOf('_');
var [first, second] = [str.slice(0, index), str.slice(index + 1)];

//first = "good"
//second = "luck_buddy"

其他回答

这个解决方案对我很有效

var str = "good_luck_buddy";
var index = str.indexOf('_');
var arr = [str.slice(0, index), str.slice(index + 1)];

//arr[0] = "good"
//arr[1] = "luck_buddy"

OR

var str = "good_luck_buddy";
var index = str.indexOf('_');
var [first, second] = [str.slice(0, index), str.slice(index + 1)];

//first = "good"
//second = "luck_buddy"

Non-regex解决方案

我运行了一些基准测试,这个解决方案获得了巨大的成功

str.slice(str.indexOf(delim) + delim.length)

// as function
function gobbleStart(str, delim) {
    return str.slice(str.indexOf(delim) + delim.length);
}

// as polyfill
String.prototype.gobbleStart = function(delim) {
    return this.slice(this.indexOf(delim) + delim.length);
};

与其他解决方案的性能比较

唯一接近的竞争者是同一行代码,除了使用substr而不是slice。

我尝试的其他解决方案,包括拆分或regexp,性能受到了很大的影响,大约慢了2个数量级。当然,在拆分结果上使用join会增加额外的性能损失。

为什么它们变慢了?每当需要创建一个新对象或数组时,JS都必须向操作系统请求一大块内存。这个过程非常缓慢。

以下是一些通用指南,以防你在追逐基准测试:

为对象{}或数组[]分配新的动态内存(就像split所创建的那样)将在性能上付出很大的代价。 RegExp搜索更复杂,因此比字符串搜索慢。 如果你已经有一个数组,解构数组和显式索引数组一样快,而且看起来很棒。

从第一个实例移除

下面是一个解决方案,它将分割到并包括第n个实例。它没有那么快,但在OP的问题上,gobble(element, '_', 1)仍然比RegExp或split解决方案快>2倍,并且可以做更多:

/*
`gobble`, given a positive, non-zero `limit`, deletes
characters from the beginning of `haystack` until `needle` has
been encountered and deleted `limit` times or no more instances
of `needle` exist; then it returns what remains. If `limit` is
zero or negative, delete from the beginning only until `-(limit)`
occurrences or less of `needle` remain.
*/
function gobble(haystack, needle, limit = 0) {
  let remain = limit;
  if (limit <= 0) { // set remain to count of delim - num to leave
    let i = 0;
    while (i < haystack.length) {
      const found = haystack.indexOf(needle, i);
      if (found === -1) {
        break;
      }
      remain++;
      i = found + needle.length;
    }
  }

  let i = 0;
  while (remain > 0) {
    const found = haystack.indexOf(needle, i);
    if (found === -1) {
      break;
    }
    remain--;
    i = found + needle.length;
  }
  return haystack.slice(i);
}

根据上面的定义,gobble('path/to/file.txt', '/')将给出文件的名称,而gobble('prefix_category_item', '_', 1)将像这个答案中的第一个解决方案一样删除前缀。


测试在macOSX 10.14上的Chrome 70.0.3538.110中运行。

ES6中获取字符串中第一个键和其余部分的简单方法是:

 const [key, ...rest] = "good_luck_buddy".split('_')
 const value = rest.join('_')
 console.log(key, value) // good, luck_buddy

你需要正则表达式和数组做什么?

myString = myString.substring(myString.indexOf('_')+1)

你好! 我的弦=我的弦。 控制台日志(myString)。

现在String.prototype.split确实允许你限制分割的数量。

str.split([分隔符[,限制]]) ... 限制可选 限制分割次数的非负整数。如果提供,则在指定分隔符的每个出现处拆分字符串,但在数组中已放入限制项时停止。数组中根本不包括任何剩余文本。 如果在达到限制之前到达字符串的末尾,则数组包含的条目可能少于限制。 如果limit为0,则不进行拆分。

警告

它可能不会像你期望的那样工作。我希望它只是忽略其余的分隔符,但相反,当它达到限制时,它再次分割剩余的字符串,从返回结果中省略分割后的部分。

let str = 'A_B_C_D_E'
const limit_2 = str.split('_', 2)
limit_2
(2) ["A", "B"]
const limit_3 = str.split('_', 3)
limit_3
(3) ["A", "B", "C"]

我希望:

let str = 'A_B_C_D_E'
const limit_2 = str.split('_', 2)
limit_2
(2) ["A", "B_C_D_E"]
const limit_3 = str.split('_', 3)
limit_3
(3) ["A", "B", "C_D_E"]