SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Too far up north
    0 Post(s)
    0 Thread(s)

    Regular expressions, slow?

    I just read an article about the test method to the regular expression object that return a boolean. Looks very powerful.

    For example, check out WebFX:s PNG Behavior that uses this.

    Would it somehow be slow to use this? I think I read somewhere that regular expressions could be slow and should not be used heavily. True?

  2. #2
    SitePoint Guru
    Join Date
    Jun 2004
    0 Post(s)
    0 Thread(s)
    It is true that complex patterns against long texts can be slow, but in general regexes are very fast. If the string to be tested is short, you will get the results almost immediately. However, it is recommended to use String.indexOf() when you can, for it is always faster than regular expressions.

  3. #3
    SitePoint Wizard
    Join Date
    Mar 2001
    0 Post(s)
    0 Thread(s)
    1) Use string functions if they will work for you. I've done some profiling of string functions versus regexes and string functions are always faster. They only find the first match, though, so if you are looking for all matches, you will have to use regexes. It seems when people learn regexes, they forget about string functions, so don't make that mistake.

    2) If string functions won't work for you, then use regexes. Some people are experts at optimizing long regexes, and different ways of coding complicated regexes are faster than others.

    Also, I think you'll find that the test() method is the least powerful of the regex functions. For instance, take a look at match() and replace(). If used as a non-global search, match() returns an array containing the matched substring, and the parethesized submatches:
    var str2 = "XYZabcd"
    var regex = /(ab)(cd)/;  //looks for first match of abcd
    var results = str.match(regex);
    alert(results[0]);  //abcd
    alert(results[1]);  //ab
    alert(results[2]);  //cd
    If used as a global search, match() returns all the matched substrings:
    var str2 = "2xyz1stuv3";
    var regex = /\d/g; //looks for any digit
    var results = str2.match(regex);
    alert(results[0]);  //2
    alert(results[1]);  //1
    alert(results[2]);  //3
    With replace(), instead of the second parameter being a string, you can have replace() call a function that you create:
    var str1 = "12xxx45yyy78"
    var regex = /\d{2}/g;  //matches all numbers that are two digits long
    var str2 = str1.replace(regex, "A");
    alert(str2);  //AxxxAyyyA
    var str = "abcd"
    var regex = /(ab)(cd)/g; //looks for all matches of abcd
    var new_str = str.replace(regex, my_func)
    //the listed arguments are automatically sent to the function:
    function my_func(matchedSubstring, parethesizedGrouping1, parenthesizedGrouping2, matchPos, str)
    	return matchedSubstring + " " + parethesizedGrouping1 + " " + parenthesizedGrouping2 + " " + matchPos + " " + str;
    alert (new_str);  //abcd cdab 0 abcd
    Last edited by 7stud; Apr 9, 2005 at 21:27.


Posting Permissions

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