I am an amateur with regular expressions, but I have a need to extract a piece of string in javascript.
My current (working) code is:
var txt='Icon/something/0.gif'; var re1='.*?'; var re2='(?:[a-z][a-z]+)'; var re3='.*?'; var re4='((?:[a-z][a-z]+))'; var p = new RegExp(re1+re2+re3+re4,["i"]); var m = p.exec(txt); var word = m[1]; //something However I can't imagine that us the most efficient way of doing it.
What I need to do is extract "something" from a string like this:
Icon/something/0.gif or "somethingelse" from:
Icon/somethingelse/10.gif Is there a more compact regex I can use?
[a-z][a-z].*?([a-z][a-z]+). Your are extracting the first substring made of lower-case letters that follows two lower-case letters and one or more arbitrary characters. To me, that seems to be not exactly what you want to do.