Udders
Udders

Reputation: 6976

Javascript regex look for a pattern

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?

Upvotes: 0

Views: 66

Answers (1)

thefourtheye
thefourtheye

Reputation: 239473

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' ]

Upvotes: 2

Related Questions