SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Addict
    Join Date
    Sep 2008
    Posts
    338
    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
    Avid Logophile silver trophy
    ParkinT's Avatar
    Join Date
    May 2006
    Location
    Central Florida
    Posts
    2,283
    Mentioned
    181 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.
    Don't be yourself. Be someone a little nicer. -Mignon McLaughlin, journalist and author (1913-1983)


    Literally, the best app for readers.
    Make Your P@ssw0rd Secure
    Leveraging SubDomains

  3. #3
    SitePoint Addict
    Join Date
    Sep 2008
    Posts
    338
    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 Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,149
    Mentioned
    14 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.
    "First make it work. Then make it better."


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
  •