My regular expression is very slow in JS

This:

/[^\(]+\.(gif|jpg|jpeg|png|svg|bmp)[^;\s]*\)/gi

is very slow in JS.

I have tested this regular expression in the same code:

/a/gi

and it runs ultra quick, so it must be the regex, not the JS code.

Any ideas to speed it up?

Instead of making it Global can you refine the string that is being evaluated, to limit the amount of data?

Are you using the RegEx for a ‘replacement’ or just a ‘match’?

What exactly are you trying to accomplish?
There may be a more efficient way to do it.

Hi,

Thanks for replying.

My Regex is very poor and I put this together the best I can using various tutorials etc.

If anything looks stupid then it probably is.

It basically searches through a CSS file and returns an array of image URLs. That’s all it needs to do. It doesn’t need to to replace anything. It works, but takes ages to complete the processing.

I’ve seen some very complicated regexps, and none of them were noticeably slow. If your code is running slowly, I strongly suspect it’s for a different reason. If you post your whole code or a link to your page, we could do more to help you troubleshoot.