tokenize.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. define("dojox/string/tokenize", [
  2. "dojo/_base/lang",
  3. "dojo/_base/sniff"
  4. ], function(lang, has){
  5. var tokenize = lang.getObject("dojox.string", true).tokenize;
  6. tokenize = function(/*String*/ str, /*RegExp*/ re, /*Function?*/ parseDelim, /*Object?*/ instance){
  7. // summary:
  8. // Split a string by a regular expression with the ability to capture the delimeters
  9. // parseDelim:
  10. // Each group (excluding the 0 group) is passed as a parameter. If the function returns
  11. // a value, it's added to the list of tokens.
  12. // instance:
  13. // Used as the "this" instance when calling parseDelim
  14. var tokens = [];
  15. var match, content, lastIndex = 0;
  16. while(match = re.exec(str)){
  17. content = str.slice(lastIndex, re.lastIndex - match[0].length);
  18. if(content.length){
  19. tokens.push(content);
  20. }
  21. if(parseDelim){
  22. if(has("opera")){
  23. var copy = match.slice(0);
  24. while(copy.length < match.length){
  25. copy.push(null);
  26. }
  27. match = copy;
  28. }
  29. var parsed = parseDelim.apply(instance, match.slice(1).concat(tokens.length));
  30. if(typeof parsed != "undefined"){
  31. tokens.push(parsed);
  32. }
  33. }
  34. lastIndex = re.lastIndex;
  35. }
  36. content = str.slice(lastIndex);
  37. if(content.length){
  38. tokens.push(content);
  39. }
  40. return tokens;
  41. };
  42. return tokenize;
  43. });