1

Still learning regex here but I need a little help.

I have this string assets/BigBlueSomething_tn.jpg

I need to match this BigBlueSomething

Closest I got is this `[^\/][a-zA-Z]+_

Problem is that I keep matching the underscore I've tried this [^\/][a-zA-Z]+[^_] at the advice from several other threads but to no avail.

I'm writing in javascript, I believe there are some regex things that dont work in that language.

Jon7
  • 7,165
  • 2
  • 33
  • 39
Spartan9209
  • 549
  • 5
  • 10
  • 1
    What precisely is the criteria to find the part you want? Everyting between the `/` and `_`? Look at the description of "lookaround operators" at [www.regular-rexpressions.info](http://www.regular-expressions.info/) – Barmar Apr 05 '13 at 23:40

3 Answers3

1

This should give you what you need:

var str = 'assets/BigBlueSomething_tn.jpg';
var match = str.split('/')[1].match(/[a-z]+/i)[0];

Edit: I used split because otherwise you'd have to loop the matches to extract what you need. match doesn't capture groups.

'assets/BigBlueSomething_tn.jpg'
       -^---------------------^- // .split('/')[1]
       -^--------------^- // .match(/[a-z]+/i)[0]

[a-z] // match any character from a to z
+ // at least one time
/i // case-insensitive to match A-Z as well
elclanrs
  • 92,861
  • 21
  • 134
  • 171
1

Try this:

var text = "assets/BigBlueSomething_tn.jpg";
var matches = /assets\/(.+?)_tn.jpg/gi.exec(text);
if(matches && matches[1]) {
    var str = matches[1];
}
VNO
  • 3,645
  • 1
  • 16
  • 25
0
/^(?:.*\/)?([0-9a-z]+)(?:_[a-z]+)?\.(?:jpg|jpeg|png|gif)$/i

This regex matches "BigBlueSomething" in the following strings:

'assets/BigBlueSomething_tn.jpg' 
'BigBlueSomething_tn.jpg'
'BigBlueSomething_TN.jPeG'
'dir1/dir2/BigBlueSomething_tn.gif'
'BigBlueSomething.png'
'BigBlueSomething_thumb.png'
'directory/BigBlueSomething_thumb.JPEG'

See this fiddle.

Alex Shesterov
  • 26,085
  • 12
  • 82
  • 103