1

I have a load of strings like this:

.ls-resourceIcon .icon_xls_gif, .ls-resourceIcon .icon_xlt_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?

1 Answer 1

5

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Ah I see. I'm getting the result ".ls-resourceIcon .xls,, .ls-resourceIcon .xlt,, .ls-resourceIcon .xlw," from your example though.
Got it, var regex = new RegExp("\.ls-resourceIcon \.icon_([A-Za-z]*)_gif,?", "g");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.