-3

I am creating a JavaScript application and I need a regex for the following thing:

I have different strings that look like:

"./resources/red_B.png" or "./resources/red_1.png"

I want to get the character after the red_ text, so I want a Regex that returns the character B or 1.

I am not a Regex star so I was wondering if someone knows the solution to the problem.

Pal Kerecsenyi
  • 560
  • 4
  • 18
Markie Doe
  • 115
  • 1
  • 9
  • 2
    What is your regex thus far? Post that. Show us where it's failing. Have you used sites like https://regexr.com ? – ProEvilz Jun 17 '18 at 14:59

3 Answers3

1

Try Regex: \/resources\/red_\K(\w)

Demo

Matt.G
  • 3,586
  • 2
  • 10
  • 23
0

i guess this is what you want:

red_(\w)+\.

catch it with groups in js as said in this link

Stick
  • 25
  • 5
0

You could match ./resources/red_ followed by capturing in a group not a dot using a negated character class ([^.]+)

\.\/resources\/red_([^.]+)

const strings = [
  "./resources/red_B.png",
  "./resources/red_1.png"
];
let pattern = /\.\/resources\/red_([^\.]+)/;
strings.forEach((s) => {
  console.log(s.match(pattern)[1]);
});
The fourth bird
  • 154,723
  • 16
  • 55
  • 70