rx1.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. ////////////////////////
  6. //Note RegExp.lastIndex does not exist any more, returning undefined is expected behaviour
  7. function write(o) {
  8. try {
  9. document.write(o + "<br/>");
  10. write = function(o) { document.write(o + "<br/>"); };
  11. } catch (ex) {
  12. try {
  13. WScript.Echo("" + o);
  14. write = function(o) { WScript.Echo("" + o); };
  15. } catch (ex2) {
  16. print("" + o);
  17. write = function(o) { print("" + o); };
  18. }
  19. }
  20. }
  21. var re;
  22. var arr;
  23. var str;
  24. write("******** replace tests");
  25. str = "JavaScript is more fun than Java";
  26. var strResult = str.replace(/\b\w+\b/g, function(word) {
  27. return word.substring(0, 1).toUpperCase() +
  28. word.substring(1);
  29. });
  30. write(strResult);
  31. str = "Doe, John"
  32. write (str.replace(/(\w+)\s*,\s*(\w+)/, "$2 $1"));
  33. str = "$1,$2";
  34. write(str.replace(/(\$(\d))/g, "$$1-$1$2"));
  35. str = "Doe, John Abe, gold C,B alan, bart"
  36. write ("original string: " + str + " replaced string: " +str.replace(new RegExp( '(\\w+)\\s*,\\s*(\\w+)', "g"), "$2 $1"));
  37. write ("original string: " + str + " function replaced string: " +str.replace(new RegExp( '(\\w+)\\s*,\\s*(\\w+)', "g"), function(word, c1, c2, offset, org) {
  38. write( "fn trace : matched str: " + word + " capture1: " + c1 + " capture2: " + c2 + " offset: " + offset + " org str: " + org);
  39. return word.substring(0, 1).toUpperCase() + word.substring(1);
  40. }));
  41. str = "a b";
  42. write ("original string: " + str + " replaced string: " +str.replace(/\s*(\s|$)\s*/g, "$1"));
  43. write("******** split tests");
  44. str = "hello <b>world</b>";
  45. re = /(<[^>]*>)/;
  46. arr = str.split(re); // Returns ["hello ","<b>","world","</b>",""]
  47. WriteAllProps(re, arr);
  48. re = /\s*,\s*/;
  49. arr = "1, 2, 3, 4, 5".split(/\s*,\s*/);
  50. WriteAllProps(re, arr);
  51. re = /a*?/;
  52. arr = "ab".split(re);
  53. WriteAllProps(re, arr);
  54. re = /a*/;
  55. arr = "ab".split(re);
  56. WriteAllProps(re, arr);
  57. re = /<(\/)?([^<>]+)>/;
  58. arr = "A<B>bold</B>and<CODE>coded</CODE>".split(re);
  59. WriteAllProps(re, arr);
  60. write("******** string.match, regexp.exec tests ");
  61. re = new RegExp("d(b+)(d)", "ig");
  62. str = "cdbBdbsbdbdz";
  63. arr = re.exec(str);
  64. WriteAllProps(re, arr);
  65. arr = str.match(re);
  66. WriteAllProps(re, arr);
  67. re.lastIndex = 7;
  68. arr = re.exec(str);
  69. WriteAllProps(re, arr);
  70. re = new RegExp("d(b+)(d)", "i");
  71. arr = str.match(re);
  72. WriteAllProps(re, arr);
  73. s = "cdbBdbsbdbdz";
  74. r = s.match();
  75. write('result match empty: ' + r);
  76. var pattern = /Java/g;
  77. var text = "JavaScript is more fun than Java!";
  78. var result;
  79. while ((result = pattern.exec(text)) != null) {
  80. write("Matched '" + result[0] + "'" +
  81. " at position " + result.index +
  82. "; next search begins at " + pattern.lastIndex);
  83. }
  84. pattern = /dddd|ddd|dd|d|MMMM|MMM|MM|M|yyyy|yy|y|hh|h|HH|H|mm|m|ss|s|tt|t|fff|ff|f|zzz|zz|z/g;
  85. result = pattern.exec("MM/dd/yyyy");
  86. write("Result matching MMMM|MMM|MM|M with MM/dd/yyyy: " + result);
  87. pattern = /aa|a/;
  88. result = pattern.exec("aa");
  89. write("Result match aa|a with aa: " + result);
  90. write("******** test empty regex expressions.");
  91. var r0 = new RegExp("");
  92. write(r0.test("foo"))
  93. var r1 = new RegExp();
  94. write(r1.test("foo"))
  95. write(r1.test(""))
  96. write("******** search tests");
  97. var re1 = /[Jj]ava([Ss]cript)?(?=\:)/;
  98. write(re1.test("JavaScript: The Definitive Guide"))
  99. re = new RegExp("d(b+)(d)", "ig");
  100. r = s.search(re); //Search the string.
  101. write('result search: ' + r);
  102. write(RegExp.input);
  103. write(RegExp.$_);
  104. s = "The rain in Spain falls mainly in the plain.";
  105. re = /falls/mi; //Create regular expression pattern.
  106. r = s.search(re); //Search the string.
  107. write('result search: ' + r);
  108. var re1 = /[Jj]ava([Ss]cript)?(?=\:)/;
  109. write(re1.test("JavaScript: The Definitive Guide"))
  110. function SearchDemo(s, re) {
  111. var r; //Declare variables.
  112. r = s.search(re); //Search the string.
  113. write('result search: ' + r);
  114. WriteAllProps(re, null);
  115. //delete RegExp.input;
  116. RegExp.input = "foooooooooo";
  117. WriteAllProps(re, null);
  118. return (r); //Return the Boolean result.
  119. }
  120. SearchDemo("The rain in Spain falls mainly in the plain.", /falls/mi);
  121. SearchDemo("1@2@3@4@5@6@7@8@9@1@523", /(\d)@(\d)@(\d)@(\d)@(\d)@(\d)@(\d)@(\d)@(\d)@(\d)@(\d+)/g);
  122. write("******** Match Demo indirect names");
  123. function matchDemo(re, pat, v1, v2, v3) {
  124. var s;s
  125. var str = "cdbBdbsbdbdz";
  126. var arr = re.exec(str);
  127. s = "first dolar arg contains: " + pat[v1] + "\n";
  128. s += "second dolar arg contains: " + pat[v2] + "\n";
  129. s += "third dolar arg contains: " + pat[v3];
  130. return (s);
  131. }
  132. // function matchDemo() {
  133. // var s;
  134. // var re = new RegExp("d(b+)(d)", "ig");
  135. // var str = "cdbBdbsbdbdz";
  136. // var arr = re.exec(str);
  137. // s = "$1 contains: " + RegExp.$1 + "\n";
  138. // s += "$2 contains: " + RegExp.$2 + "\n";
  139. // s += "$3 contains: " + RegExp.$3;
  140. // return (s);
  141. // }
  142. var re = new RegExp("d(b+)(d)", "ig");
  143. var sresult = matchDemo(re, RegExp, "$" + "1", "$" + "2", "$" + "3");
  144. write("reg match: " + sresult);
  145. write("******** Misc");
  146. var x = /abc/;
  147. write(x.toString());
  148. write("javscriptSting".search(/[utg]/));
  149. write("jaaaavscriptSaating".match(/a+/));
  150. write("jaaaavscriptSaating".match(/a+?/g));
  151. function WriteAllProps(reg, arr) {
  152. write('regObjInst.lastIndex: ' + reg.lastIndex);
  153. write('regObjInst.Source: ' + reg.source);
  154. write('regObjInst.global: ' + reg.global);
  155. write('regObjInst.ignoreCase: ' + reg.ignoreCase);
  156. write('regObjInst.multiline: ' + reg.multiline);
  157. write('regObjInst.options: ' + reg.options);
  158. write('RegExp.input: ' + RegExp.input);
  159. write('RegExp.input, $_: ' + RegExp.$_);
  160. write('RegExp.index: ' + RegExp.index);
  161. write('RegExp.lastIndex: ' + RegExp.lastIndex);
  162. write('RegExp.lastMatch: ' + RegExp.lastMatch);
  163. write('RegExp.lastMatch, $&: ' + RegExp['$&']);
  164. write('RegExp.lastParen: ' + RegExp.lastParen);
  165. write('RegExp.lastParen $+: ' + RegExp['$+']);
  166. write('RegExp.leftContext: ' + RegExp.leftContext);
  167. write('RegExp.leftContext $`: ' + RegExp['$`']);
  168. write('RegExp.rightContext: ' + RegExp.rightContext);
  169. write('RegExp.rightContext $\': ' + RegExp["$'"]);
  170. write('RegExp.$1: ' + RegExp.$1);
  171. write('RegExp.$2: ' + RegExp.$2);
  172. write('RegExp.$3: ' + RegExp.$3);
  173. write('RegExp.$4: ' + RegExp.$4);
  174. write('RegExp.$5: ' + RegExp.$5);
  175. write('RegExp.$6: ' + RegExp.$6);
  176. write('RegExp.$7: ' + RegExp.$7);
  177. write('RegExp.$8: ' + RegExp.$8);
  178. write('RegExp.$9: ' + RegExp.$9);
  179. write("");
  180. if (arr) {
  181. write('Array : ' + arr);
  182. write('Array input: ' + arr.input);
  183. write('Array index: ' + arr.index);
  184. write('Array lastIndex: ' + arr.lastIndex);
  185. }
  186. write("");
  187. }