0

I am struggling with javascript regex (regex in general actually).

I have a string of text looks like this,

wall_treatment/S3/COOL_NEUTRAL/MMC_Walls_FC_S1_COOL_NEUTRAL_00000.png

In the string above I want to look for a string that matches Sn (where n is a number). Is this possible?

2
  • Could it be S12, S01 or even S001003 ? Commented Jan 7, 2014 at 12:57
  • Could you explain why you need that? What are you doing with this path? Commented Jan 7, 2014 at 13:15

1 Answer 1

2
var data="wall_treatment/S3/COOL_NEUTRAL/MMC_Walls_FC_S1_COOL_NEUTRAL_00000.png";
console.log(/S\d/.exec(data)[0]);

will give you the first match S3. If you want all the instances, you can do

console.log(data.match(/S\d/g));

will give all the matches and in this case it is

[ 'S3', 'S1' ]
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.