SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Thread: My regular expression is very slow in JS

  1. #1
    SitePoint Addict
    Join Date
    Sep 2008
    Posts
    337
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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?

  2. #2
    Community Advisor silver trophy
    ParkinT's Avatar
    Join Date
    May 2006
    Location
    Central Florida
    Posts
    1,641
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    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.

  3. #3
    SitePoint Addict
    Join Date
    Sep 2008
    Posts
    337
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  4. #4
    SitePoint Guru bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    737
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    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.
    "Folks who know what they're doing make complexity seem simple."

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •