【问题标题】:How to get the part of the string after the 2nd space [duplicate]如何获取第二个空格之后的字符串部分[重复]
【发布时间】:2021-08-13 08:35:04
【问题描述】:

我有一个类似的字符串:$m-set 88829828277 very good he is。从这个字符串我想得到第二个空格之后的部分。即,我想得到:very good he is
我尝试使用split(" ")[2],但它只给出一个词:very

非常感谢任何帮助!
谢谢!

【问题讨论】:

    标签: javascript string


    【解决方案1】:

    虽然你可以拆分和加入:

    const input = '$m-set 88829828277 very good he is';
    const splits = input.split(' ');
    const output = splits.slice(2).join(' ');
    console.log(output);

    你也可以使用正则表达式:

    const input = '$m-set 88829828277 very good he is';
    const output = input.match(/\S+ \S+ (.+)/)[1];
    console.log(output);

    (.+) 将所有内容放在捕获组中第二个空格之后,[1] 从匹配项中访问捕获组。

    【讨论】:

      猜你喜欢
      • 2017-05-06
      • 2015-06-06
      • 1970-01-01
      • 2016-05-03
      • 2023-04-01
      • 2021-10-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多