captures.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. // Test of regex captures with some examples motivated by WAC.
  6. WScript.Echo(/(ab)|(ac)/.exec("aabc"));
  7. WScript.Echo(/(ab)|(ac)+/.exec("qacbacacd"));
  8. WScript.Echo(/(ab)+|(ac)+/.exec("qababacacd"));
  9. WScript.Echo(/(?:ab)+|(ac)+/.exec("qababacacd"));
  10. WScript.Echo(/(?:ab)+|(?:ac)+/.exec("qababacacd"));
  11. var a = new RegExp('^([^?]+)');
  12. var a1 = a.exec("file://d:\\foo.txt");
  13. WScript.Echo(a1);
  14. var b = new RegExp('^([a-z+.-]+://(?:[^/]*/)*)[^/]*$');
  15. var b1 = b.exec("file://d:\\foo.txt");
  16. WScript.Echo(b1);
  17. var c = b.exec(a.exec("file://d:\\foo.txt")[1])[1];
  18. WScript.Echo(c);
  19. var http = "http://ezedev/WAC/onenoteframe.aspx?Fi=c%3A%5CViewingData%5Cbasefile%5CTest&H=ol&C=0__ezedev&ui=en-US";
  20. WScript.Echo(a.exec(http));
  21. WScript.Echo(b.exec(a.exec(http)[1]));
  22. var d = b.exec(a.exec(http)[1])[1];
  23. WScript.Echo(d);
  24. var d = 'foo bar'.replace(/(foo)|(bar)/g, '[$1$2]');
  25. WScript.Echo(d+"");
  26. var e = "ab".replace(/(.)(.){0}/,'$1$2');
  27. WScript.Echo(e+"");
  28. var groups = {};
  29. function Assert(actual, expected, category, notStrict)
  30. {
  31. if (!groups[category]) {
  32. groups[category] = 1;
  33. } else {
  34. groups[category]++;
  35. }
  36. var condition = (actual === expected);
  37. if (!!notStrict) {
  38. condition = (actual == expected)
  39. }
  40. if (!condition) {
  41. WScript.Echo(category + " test " + groups[category] + " failed. Expected: " + expected + ", Actual: " + actual);
  42. } else {
  43. WScript.Echo(category + " test " + groups[category] + " passed");
  44. }
  45. }
  46. var needle = /(bc)/;
  47. var haystack = "abcdef";
  48. haystack.match(needle);
  49. Assert(RegExp.$1, "bc", "numberedRegex", true);
  50. Assert(RegExp.$2, "", "numberedRegex");
  51. needle = /xy/;
  52. haystack.match(needle);
  53. Assert(RegExp.$1, "bc", "numberedRegex");
  54. Assert(RegExp.$2, "", "numberedRegex");