tokenize.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /*
  2. Copyright (c) 2004-2012, The Dojo Foundation All Rights Reserved.
  3. Available via Academic Free License >= 2.1 OR the modified BSD license.
  4. see: http://dojotoolkit.org/license for details
  5. */
  6. if(!dojo._hasResource["dojox.string.tokenize"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.string.tokenize"] = true;
  8. dojo.provide("dojox.string.tokenize");
  9. dojox.string.tokenize = function(/*String*/ str, /*RegExp*/ re, /*Function?*/ parseDelim, /*Object?*/ instance){
  10. // summary:
  11. // Split a string by a regular expression with the ability to capture the delimeters
  12. // parseDelim:
  13. // Each group (excluding the 0 group) is passed as a parameter. If the function returns
  14. // a value, it's added to the list of tokens.
  15. // instance:
  16. // Used as the "this" instance when calling parseDelim
  17. var tokens = [];
  18. var match, content, lastIndex = 0;
  19. while(match = re.exec(str)){
  20. content = str.slice(lastIndex, re.lastIndex - match[0].length);
  21. if(content.length){
  22. tokens.push(content);
  23. }
  24. if(parseDelim){
  25. if(dojo.isOpera){
  26. var copy = match.slice(0);
  27. while(copy.length < match.length){
  28. copy.push(null);
  29. }
  30. match = copy;
  31. }
  32. var parsed = parseDelim.apply(instance, match.slice(1).concat(tokens.length));
  33. if(typeof parsed != "undefined"){
  34. tokens.push(parsed);
  35. }
  36. }
  37. lastIndex = re.lastIndex;
  38. }
  39. content = str.slice(lastIndex);
  40. if(content.length){
  41. tokens.push(content);
  42. }
  43. return tokens;
  44. }
  45. }