I have a load of strings like this:
.ls-resourceIcon .icon_xls_gif, .ls-resourceIcon .icon_xlt开发者_JS百科_gif, .ls-resourceIcon .icon_xlw_gif
I want to get the strings between icon_ and _gif into a comma separated list, so in this case "xls,xlt,xlw," (I can trim the trailing comma).
I have so far got this:
var regex = new RegExp("^.*icon_(.*)_gif.*$", "g");
var result = input.replace(regex, "$1,");
but that gives me
xlw,
as a result, not all the matches.
What am I missing? Is there an easier way to do this that I haven't noticed?
Your regex is greedy, so the leading .*
will grab everything up until the final icon_xlw_gif
. You need to make both sides non-greedy. This might work:
var regex = new RegExp("icon_([A-Za-z]*)_gif", "g");
Remove the leading and trailing .*
Also replaced the (.*)
with I think also now wouldn't work the way you intended it.
精彩评论