所以記錄一下在 Javascript 中的用法
<script language="javascript">
var str = "46 Ladys and Gentleman~\nWelcome 12 to the show !";
document.write("Test string : <br>" + str.replace("\n","<br>") + "<br><br>");
var RegStr_NoGlobal = /\d/;
/* 使用g(Global)參數時,RegExp在做test()或exec()時 */
/* 會保存一個指標lastIndex,記錄上次查詢結束的位置。 */
/* 第一次test()後,lastIndex就指向字串結尾 */
/* 第二次test()由結尾開始查,啥都查不到,就傳回false,*/
/* 然後再把lastIndex重設為0。 */
var RegStr_Global = /\d/g;
var RegStr_NoIgnoreCase = /wel/;
var RegStr_IgnoreCase = /wel/i;
var RegStr_NoMultiLine = /^Wel/;
var RegStr_MultiLine = /^Wel/m;
var RegStr_Variable = /[a-z]+([a-z]{2}) /gi;
var result;
result = RegStr_NoGlobal.exec(str);
document.write("No-Global Round1 : " + result + "<br>");
result = RegStr_NoGlobal.exec(str);
document.write("No-Global Round2 : " + result + "<br>");
result = RegStr_Global.exec(str);
document.write ("Global Round1 : " + result + "<br>");
result = RegStr_Global.exec(str);
document.write ("Global Round2 : " + result + "<br><br>");
result = RegStr_NoIgnoreCase.exec(str);
document.write("No-IgnoreCase : " + result + "<br>");
result = RegStr_IgnoreCase.exec(str);
document.write("IgnoreCase : " + result + "<br><br>");
result = RegStr_NoMultiLine.exec(str);
document.write("No-MultiLine : " + result + "<br>");
result = RegStr_MultiLine.exec(str);
document.write("MultiLine : " + result + "<br><br>");
result = RegStr_Variable.exec(str);
document.write("Match string : " + result[0] + " , Match variable : " + result[1] + "<br>");
result = RegStr_Variable.exec(str);
document.write("Match string : " + result[0] + " , Match variable : " + result[1] + "<br>");
result = RegStr_Variable.exec(str);
document.write("Match string : " + result[0] + " , Match variable : " + result[1] + "<br>");
result = RegStr_Variable.exec(str);
document.write("Match string : " + result[0] + " , Match variable : " + result[1] + "<br>");
result = RegStr_Variable.exec(str);
document.write("Match string : " + result[0] + " , Match variable : " + result[1] + "<br>");
</script>
var str = "46 Ladys and Gentleman~\nWelcome 12 to the show !";
document.write("Test string : <br>" + str.replace("\n","<br>") + "<br><br>");
var RegStr_NoGlobal = /\d/;
/* 使用g(Global)參數時,RegExp在做test()或exec()時 */
/* 會保存一個指標lastIndex,記錄上次查詢結束的位置。 */
/* 第一次test()後,lastIndex就指向字串結尾 */
/* 第二次test()由結尾開始查,啥都查不到,就傳回false,*/
/* 然後再把lastIndex重設為0。 */
var RegStr_Global = /\d/g;
var RegStr_NoIgnoreCase = /wel/;
var RegStr_IgnoreCase = /wel/i;
var RegStr_NoMultiLine = /^Wel/;
var RegStr_MultiLine = /^Wel/m;
var RegStr_Variable = /[a-z]+([a-z]{2}) /gi;
var result;
result = RegStr_NoGlobal.exec(str);
document.write("No-Global Round1 : " + result + "<br>");
result = RegStr_NoGlobal.exec(str);
document.write("No-Global Round2 : " + result + "<br>");
result = RegStr_Global.exec(str);
document.write ("Global Round1 : " + result + "<br>");
result = RegStr_Global.exec(str);
document.write ("Global Round2 : " + result + "<br><br>");
result = RegStr_NoIgnoreCase.exec(str);
document.write("No-IgnoreCase : " + result + "<br>");
result = RegStr_IgnoreCase.exec(str);
document.write("IgnoreCase : " + result + "<br><br>");
result = RegStr_NoMultiLine.exec(str);
document.write("No-MultiLine : " + result + "<br>");
result = RegStr_MultiLine.exec(str);
document.write("MultiLine : " + result + "<br><br>");
result = RegStr_Variable.exec(str);
document.write("Match string : " + result[0] + " , Match variable : " + result[1] + "<br>");
result = RegStr_Variable.exec(str);
document.write("Match string : " + result[0] + " , Match variable : " + result[1] + "<br>");
result = RegStr_Variable.exec(str);
document.write("Match string : " + result[0] + " , Match variable : " + result[1] + "<br>");
result = RegStr_Variable.exec(str);
document.write("Match string : " + result[0] + " , Match variable : " + result[1] + "<br>");
result = RegStr_Variable.exec(str);
document.write("Match string : " + result[0] + " , Match variable : " + result[1] + "<br>");
</script>
沒有留言:
張貼留言