Jon Sud
Jon Sud

Reputation: 11671

How to extract string between dash with regex in javascript?

I have a string in 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

and I want to extract the SOME-.. until the first - char.

I use this regex but didn't work. what is the correct regex?

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 });
      
      

Upvotes: 1

Views: 879

Answers (5)

Martijn
Martijn

Reputation: 16123

In some steps to explain regex:

const s = "bugfix/SOME-9234-add-company";

// This one is close, but will return only ONE digit 
console.log( s.match(/SOME-([1-9])/) );
// What you wanted:
console.log( s.match(/SOME-([1-9]+)/) ); // note the +, meaning '1 or more'

// Explanation about your other two tries: 
// 1. This'll give you everything after 'SOME':
console.log( s.match(/SOME-(.*)/) );
// 2. This'll match if the line 'ends with -' (= -$)
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+)/) );

Upvotes: 3

The fourth bird
The fourth bird

Reputation: 163632

In the patterns that you tried:

  • $ asserts the end of the string,
  • [1-9] matches a single digit 1-9 without the 0
  • .* will match any characters without a newline 0+ times

There is no need for capturing groups, you could match:

\bSOME-\d+

See a regex demo

Note that match will return an array, from which you could take the 0 index.

[
  "bugfix/SOME-9234-add-company",
  "SOME/SOME-933234-add-company",
  "test/SOME-5559234-add-company"

].forEach(s => console.log(s.match(/\bSOME-\d+/)[0]));

Upvotes: 0

4nkitpatel
4nkitpatel

Reputation: 178

If You don't want to use regex then this could be simple

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"

Upvotes: 0

Alessio Cantarella
Alessio Cantarella

Reputation: 5211

You could use the /(SOME-[\d]+)/g regex, e.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]);
});

Upvotes: 2

Pac0
Pac0

Reputation: 23174

/(SOME-[^-]+)/ should work well. (capture everything what is not an hyphen after SOME-)

Or, if you know you have only digits, close to what you tried:

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

You were missing a + to take more than one character.

I also changed the parenthesis to capture exactly what you show in the question (i.e., including the part with SOME)

Upvotes: 0

Related Questions