【问题标题】:Split String from substring for first match从子字符串中拆分字符串以进行第一次匹配
【发布时间】:2021-10-06 14:14:21
【问题描述】:

下面是一个字符串示例,我想将它从子字符串中拆分为 3 个部分

test01String001s

例子:

  • example_1 subString == "0"

    res: [ "test","0","1String001s" ] - 测试01String001s

  • example_2 subString == "st"

    res: [ "te","st","01String001s" ] - test01String001s

  • example_3 subString == "01s"

    res: [ "test01String0","01s", "" ] - test01String001s

  • example_4 subString == "t"

    res: [ "","t","est01String001s", ] - test01String001s

我尝试通过

解决
 var string='test01String001s'
 var keyword="01"
 console.log(string.split(keyword));

输出:

     ["test","String0","s"]  instead of [ "test","01","String001s"]

 var string='test01String001s'
 var keyword="0"
 console.log(string .split(keyword, 2));

输出

 ["test","1String"]

【问题讨论】:

  • 这能回答你的问题吗? Split string once in javascript?
  • 其实不完全。我只是在评论中添加一个答案,但我认为我们很接近
  • 要保留分隔符,可以使用[s.slice(0,i), separator, s.slice(i+1)]

标签: javascript string split


【解决方案1】:

要获得 3 个部分,我不会使用 split,而是使用一些逻辑

const mystring = 'test01String001s'
const sp = ['01','1','t','st', '1s']

sp.forEach(item=>{
  let idx=mystring.indexOf(item)
  let result=[]
  if(idx===0){
    result.push('')
    result.push(item)
    result.push(mystring.slice(item.length))
  }else if (idx+item.length ==mystring.length){
    result.push(mystring.slice(0,idx))
    result.push(item)
    result.push('')
  }else if(idx!=-1){
    result.push(mystring.slice(0,idx))
    result.push(item)
    result.push(mystring.slice(idx+item.length))
  }
  console.log(result)
})

【讨论】:

    【解决方案2】:

    试试这个。

    var string = "test01String001s";
    var splitKey = '01';
    var components = string.split(splitKey);
    var array = ([components.shift(), components.join(splitKey)]);
    array.splice(1, 0, splitKey);
    console.log(array.join());

    输出:test,01,String001s

    【讨论】:

    • 很好用!感谢您的帮助。
    • @NakosKotsanis 很高兴为您提供帮助。如果对你有帮助,请采纳
    【解决方案3】:

    这应该可行:

    const test = 'test01String001s';
    const key = "t";
    const re = new RegExp('(.*?)(' + key + ')(.*)');
    const result = test.match(re, "regex");
    result.shift();
    
    console.log(result);
    

    所以这个正则表达式匹配返回一个数组:

    (.*?) - 键之前的任何内容

    (' + 键 + ') - 键

    (.*) - 键后的任何内容

    不幸的是它还返回了整个匹配的单词,所以我使用 shift 从数组中删除了第一个匹配项。

    【讨论】:

      【解决方案4】:

      我编写了一个名为split 的函数。如果在字符串中找到 splitter 就可以正常工作,否则返回[string, splitter, string]

      例如:

      string = 'test01String001ssplitter=01 现在给出输出 ["test", "0", "1String001s"]

      如果splitter = "apple" 它将返回["test01String001s", "apple", "test01String001s"]

      如果您想在字符串中找不到拆分器时返回空值, 只需在split 函数中的当前返回语句行之前添加if(splitter_index < 0) return ["", "", ""]

      请检查下面的所有示例,任何认为需要更改的人请发表评论。

      let string = "test01String001s"
      
      function split(string, splitter) {
          let splitter_index = string.indexOf(splitter);
          
          return [string.slice(0, splitter_index), splitter, string.slice(splitter_index+splitter.length)];
      }
      
      // example_1
      console.log(split(string, '0'));
      
      // example_2
      console.log(split(string, 'st'));
      
      // example_3
      console.log(split(string, '01s'));
      
      // example_4
      console.log(split(string, 't'));

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-10-12
        • 2017-12-15
        • 1970-01-01
        • 2019-07-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多