【问题标题】:How to extract string between dash with regex in javascript?如何在javascript中用正则表达式提取破折号之间的字符串?
【发布时间】:2020-06-22 08:13:11
【问题描述】:

我在 javascript 中有一个字符串:

 const str = "bugfix/SOME-9234-add-company"; // output should be SOME-9234
 const str2 = "SOME/SOME-933234-add-company"; // output should be SOME-933234
 const str3 = "test/SOME-5559234-add-company"; // output should be SOME-5559234

我想提取 SOME-.. 直到第一个 - 字符。

我使用了这个正则表达式,但没有用。什么是正确的正则表达式?

const s = "bugfix/SOME-9234-add-company";
const r1 = s.match(/SOME-([1-9])/);
const r2 = s.match(/SOME-(.*)/);
const r3 = s.match(/SOME-(.*)-$/);

console.log({ r1, r2, r3 });
      
      

【问题讨论】:

    标签: javascript regex string


    【解决方案1】:

    您可以使用/(SOME-[\d]+)/g 正则表达式,例如

    const strings = [
      "bugfix/SOME-9234-add-company", // output should be SOME-9234
      "SOME/SOME-933234-add-company", // output should be SOME-933234
      "test/SOME-5559234-add-company" // output should be SOME-5559234
    ];
    strings.forEach(string => {
      const regex = /(SOME-[\d]+)/g;
      const found = string.match(regex);
      console.log(found[0]);
    });

    【讨论】:

      【解决方案2】:

      const s = "bugfix/SOME-9234-add-company";
      
      // This one is close, but will return only on digit 
      console.log( s.match(/SOME-([1-9])/) );
      // What you wanted:
      console.log( s.match(/SOME-([1-9]+)/) ); // note the +, meaning '1 or more'
      
      // these are also close. 
      // This'll give you everything after 'SOME':
      console.log( s.match(/SOME-(.*)/) );
      // This'll match if the last character of the line is a -
      console.log( s.match(/SOME-(.*)-$/) );
       
      //What you wanted:
      console.log( s.match(/SOME-(.*?)-/) ); // Not end of line, but 'ungreedy'(the ?) which does 'untill the first - you encounter'
      
      // instead of Using [1-9], you can also use \d, digits, for readability:
      console.log( s.match(/SOME-(\d+)/) );

      【讨论】:

        【解决方案3】:

        /(SOME-[^-]+)/ 应该可以正常工作。 (捕获SOME- 之后不是连字符的所有内容)

        或者,如果你知道你只有数字,那么接近你尝试过的:

        /(SOME-[1-9]+)/

        您错过了+ 以获取多个字符。

        我还更改了括号以准确捕获您在问题中显示的内容(即,包括带有SOME 的部分)

        【讨论】:

          【解决方案4】:

          如果您不想使用正则表达式,那么这可能很简单

          const s = "bugfix/SOME-9234-add-company";
          const str2 = "SOME/SOME-933234-add-company";
          const str3 = "test/SOME-5559234-add-company";
          const r1 = s.split("/")[1].split("-",2).join("-"); // "SOME-9234"
          const r2 = str2.split("/")[1].split("-",2).join("-"); // "SOME-933234"
          const r3 = str3.split("/")[1].split("-",2).join("-"); // "SOME-5559234"
          

          【讨论】:

          • 虽然这些工作正常,但在字符串上使用数组操作通常是您想要远离的事情:)
          • @Martijn 为什么会这样?你能解释一下吗:)?
          • 数组操作(大多数时候)比字符串操作慢。不太确定正则表达式在该范围内的得分,但简单的字符串操作非常快。数组的开销更大。在这种情况下,这并不重要,但在项目规模上它加起来
          【解决方案5】:

          在您尝试过的模式中:

          • $ 断言字符串的结尾,
          • [1-9] 匹配没有 0 的单个数字 1-9
          • .* 将匹配任何没有换行符的字符 0+ 次

          不需要捕获组,你可以匹配:

          \bSOME-\d+
          

          查看regex demo

          请注意,match 将返回一个数组,您可以从中获取 0 索引。

          [
            "bugfix/SOME-9234-add-company",
            "SOME/SOME-933234-add-company",
            "test/SOME-5559234-add-company"
          
          ].forEach(s => console.log(s.match(/\bSOME-\d+/)[0]));

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2012-08-23
            • 2022-01-17
            • 1970-01-01
            • 1970-01-01
            • 2013-07-20
            相关资源
            最近更新 更多