简单的说,正则表达式是一种可以用于模式匹配和替换的强有力的工具。其作用如下:
测试字符串的某个模式。例如,可以对一个输入字符串进行测试,看在该字符串是否存在一个电话号码模式或一个信用卡号码模式。这称为数据有效性验证。
http://paradise.ezla.com.tw/files/article/html/228/228553/8581143.html
http://paradise.ezla.com.tw/files/article/html/228/228554/8581153.html
http://paradise.ezla.com.tw/files/article/html/228/228553/8581154.html
http://paradise.ezla.com.tw/files/article/html/228/228552/8581156.html
http://paradise.ezla.com.tw/files/article/html/228/228552/8581157.html
http://paradise.ezla.com.tw/files/article/html/228/228552/8581161.html
http://paradise.ezla.com.tw/files/article/html/228/228554/8581163.html
http://paradise.ezla.com.tw/files/article/html/228/228553/8581167.html
http://paradise.ezla.com.tw/files/article/html/228/228553/8581170.html
http://paradise.ezla.com.tw/files/article/html/228/228554/8581171.html
http://paradise.ezla.com.tw/files/article/html/228/228554/8581172.html
http://paradise.ezla.com.tw/files/article/html/228/228552/8581175.html
http://paradise.ezla.com.tw/files/article/html/228/228553/8581176.html
http://paradise.ezla.com.tw/files/article/html/228/228554/8581182.html
http://paradise.ezla.com.tw/files/article/html/228/228553/8581183.html
http://paradise.ezla.com.tw/files/article/html/228/228552/8581187.html
http://paradise.ezla.com.tw/files/article/html/228/228554/8581189.html
http://paradise.ezla.com.tw/files/article/html/228/228552/8581190.html
http://paradise.ezla.com.tw/files/article/html/228/228554/8581192.html
http://paradise.ezla.com.tw/files/article/html/228/228553/8581195.html
http://paradise.ezla.com.tw/files/article/html/228/228552/8581196.html
http://paradise.ezla.com.tw/files/article/html/228/228553/8581199.html
http://paradise.ezla.com.tw/files/article/html/228/228553/8581199.html
http://paradise.ezla.com.tw/files/article/html/228/228554/8581201.html
http://paradise.ezla.com.tw/files/article/html/228/228554/8581204.html
http://paradise.ezla.com.tw/files/article/html/228/228553/8581205.html
http://paradise.ezla.com.tw/files/article/html/228/228552/8581206.html
http://paradise.ezla.com.tw/files/article/html/228/228554/8581208.html
http://paradise.ezla.com.tw/files/article/html/228/228553/8581210.html
http://paradise.ezla.com.tw/files/article/html/228/228554/8581212.html
http://paradise.ezla.com.tw/files/article/html/228/228554/8581214.html
http://paradise.ezla.com.tw/files/article/html/228/228552/8581217.html
http://paradise.ezla.com.tw/files/article/html/228/228552/8581218.html
http://paradise.ezla.com.tw/files/article/html/228/228552/8581223.html
http://paradise.ezla.com.tw/files/article/html/228/228552/8581223.html
http://paradise.ezla.com.tw/files/article/html/228/228552/8581226.html
http://paradise.ezla.com.tw/files/article/html/228/228554/8581229.html
http://paradise.ezla.com.tw/files/article/html/228/228553/8581227.html
http://paradise.ezla.com.tw/files/article/html/228/228554/8581230.html
http://paradise.ezla.com.tw/files/article/html/228/228552/8581232.html
http://paradise.ezla.com.tw/files/article/html/228/228553/8581233.html
http://paradise.ezla.com.tw/files/article/html/228/228552/8581236.html
http://paradise.ezla.com.tw/files/article/html/228/228554/8581237.html
http://paradise.ezla.com.tw/files/article/html/228/228553/8581240.html
http://paradise.ezla.com.tw/files/article/html/228/228553/8581240.html
http://paradise.ezla.com.tw/files/article/html/228/228553/8581241.html
http://paradise.ezla.com.tw/files/article/html/228/228553/8581244.html
http://paradise.ezla.com.tw/files/article/html/228/228549/8581095.html
http://paradise.ezla.com.tw/files/article/html/228/228549/8581096.html
http://paradise.ezla.com.tw/files/article/html/228/228550/8581097.html
http://paradise.ezla.com.tw/files/article/html/228/228550/8581098.html
http://paradise.ezla.com.tw/files/article/html/228/228550/8581099.html
http://paradise.ezla.com.tw/files/article/html/228/228549/8581100.html
http://paradise.ezla.com.tw/files/article/html/228/228551/8581101.html
http://paradise.ezla.com.tw/files/article/html/228/228550/8581102.html
http://paradise.ezla.com.tw/files/article/html/228/228550/8581104.html
http://paradise.ezla.com.tw/files/article/html/228/228551/8581103.html
http://paradise.ezla.com.tw/files/article/html/228/228551/8581105.html
http://paradise.ezla.com.tw/files/article/html/228/228550/8581107.html
http://paradise.ezla.com.tw/files/article/html/228/228549/8581108.html
http://paradise.ezla.com.tw/files/article/html/228/228549/8581108.html
http://paradise.ezla.com.tw/files/article/html/228/228550/8581109.html
http://paradise.ezla.com.tw/files/article/html/228/228551/8581110.html
http://paradise.ezla.com.tw/files/article/html/228/228549/8581111.html
http://paradise.ezla.com.tw/files/article/html/228/228551/8581112.html
http://paradise.ezla.com.tw/files/article/html/228/228549/8581113.html
http://paradise.ezla.com.tw/files/article/html/228/228549/8581114.html
http://paradise.ezla.com.tw/files/article/html/228/228550/8581115.html
http://paradise.ezla.com.tw/files/article/html/228/228550/8581117.html
http://paradise.ezla.com.tw/files/article/html/228/228549/8581118.html
http://paradise.ezla.com.tw/files/article/html/228/228549/8581118.html
http://paradise.ezla.com.tw/files/article/html/228/228549/8581119.html
http://paradise.ezla.com.tw/files/article/html/228/228551/8581120.html
http://paradise.ezla.com.tw/files/article/html/228/228550/8581121.html
http://paradise.ezla.com.tw/files/article/html/228/228550/8581122.html
http://paradise.ezla.com.tw/files/article/html/228/228551/8581123.html
http://paradise.ezla.com.tw/files/article/html/228/228550/8581124.html
http://paradise.ezla.com.tw/files/article/html/228/228550/8581124.html
http://paradise.ezla.com.tw/files/article/html/228/228549/8581126.html
http://paradise.ezla.com.tw/files/article/html/228/228549/8581127.html
http://paradise.ezla.com.tw/files/article/html/228/228550/8581128.html
http://paradise.ezla.com.tw/files/article/html/228/228551/8581130.html
http://paradise.ezla.com.tw/files/article/html/228/228551/8581132.html
http://paradise.ezla.com.tw/files/article/html/228/228551/8581132.html
http://paradise.ezla.com.tw/files/article/html/228/228550/8581129.html
http://paradise.ezla.com.tw/files/article/html/228/228549/8581133.html
http://paradise.ezla.com.tw/files/article/html/228/228551/8581134.html
http://paradise.ezla.com.tw/files/article/html/228/228551/8581135.html
http://paradise.ezla.com.tw/files/article/html/228/228549/8581136.html
http://paradise.ezla.com.tw/files/article/html/228/228551/8581137.html
http://paradise.ezla.com.tw/files/article/html/228/228549/8581138.html
http://paradise.ezla.com.tw/files/article/html/228/228549/8581139.html
http://paradise.ezla.com.tw/files/article/html/228/228551/8581140.html
http://paradise.ezla.com.tw/files/article/html/228/228549/8581142.html
http://paradise.ezla.com.tw/files/article/html/228/228549/8581142.html
http://paradise.ezla.com.tw/files/article/html/228/228551/8581144.html
http://paradise.ezla.com.tw/files/article/html/228/228550/8581146.html
http://paradise.ezla.com.tw/files/article/html/228/228549/8581147.html
http://paradise.ezla.com.tw/files/article/html/228/228550/8581146.html
http://paradise.ezla.com.tw/files/article/html/228/228550/8581148.html
http://paradise.ezla.com.tw/files/article/html/228/228551/8581149.html
http://paradise.ezla.com.tw/files/article/html/228/228551/8581150.html
http://paradise.ezla.com.tw/files/article/html/228/228550/8581152.html
http://paradise.ezla.com.tw/files/article/html/228/228550/8581152.html
http://paradise.ezla.com.tw/files/article/html/228/228551/8581155.html
http://paradise.ezla.com.tw/files/article/html/228/228550/8581159.html
http://paradise.ezla.com.tw/files/article/html/228/228550/8581160.html
http://paradise.ezla.com.tw/files/article/html/228/228549/8581158.html
http://paradise.ezla.com.tw/files/article/html/228/228551/8581162.html
http://paradise.ezla.com.tw/files/article/html/228/228549/8581164.html
http://paradise.ezla.com.tw/files/article/html/228/228549/8581168.html
http://paradise.ezla.com.tw/files/article/html/228/228551/8581166.html
http://paradise.ezla.com.tw/files/article/html/228/228549/8581168.html
http://paradise.ezla.com.tw/files/article/html/228/228551/8581169.html
http://paradise.ezla.com.tw/files/article/html/228/228551/8581173.html
http://paradise.ezla.com.tw/files/article/html/228/228550/8581174.html
http://paradise.ezla.com.tw/files/article/html/228/228550/8581178.html
http://paradise.ezla.com.tw/files/article/html/228/228550/8581178.html
http://paradise.ezla.com.tw/files/article/html/228/228550/8581179.html
http://paradise.ezla.com.tw/files/article/html/228/228549/8581180.html
http://paradise.ezla.com.tw/files/article/html/228/228550/8581181.html
http://paradise.ezla.com.tw/files/article/html/228/228549/8581184.html
http://paradise.ezla.com.tw/files/article/html/228/228549/8581185.html
http://paradise.ezla.com.tw/files/article/html/228/228549/8581186.html
http://paradise.ezla.com.tw/files/article/html/228/228550/8581188.html
http://paradise.ezla.com.tw/files/article/html/228/228550/8581191.html
http://paradise.ezla.com.tw/files/article/html/228/228551/8581194.html
http://paradise.ezla.com.tw/files/article/html/228/228551/8581194.html
http://paradise.ezla.com.tw/files/article/html/228/228550/8581197.html
http://paradise.ezla.com.tw/files/article/html/228/228550/8581200.html
http://paradise.ezla.com.tw/files/article/html/228/228551/8581202.html
http://paradise.ezla.com.tw/files/article/html/228/228549/8581203.html
http://paradise.ezla.com.tw/files/article/html/228/228549/8581207.html
http://paradise.ezla.com.tw/files/article/html/228/228549/8581211.html
http://paradise.ezla.com.tw/files/article/html/228/228550/8581209.html
http://paradise.ezla.com.tw/files/article/html/228/228550/8581213.html
http://paradise.ezla.com.tw/files/article/html/228/228549/8581215.html
http://paradise.ezla.com.tw/files/article/html/228/228551/8581216.html
http://paradise.ezla.com.tw/files/article/html/228/228551/8581219.html
http://paradise.ezla.com.tw/files/article/html/228/228549/8581220.html
http://paradise.ezla.com.tw/files/article/html/228/228549/8581222.html
http://paradise.ezla.com.tw/files/article/html/228/228549/8581225.html
http://paradise.ezla.com.tw/files/article/html/228/228551/8581224.html
http://paradise.ezla.com.tw/files/article/html/228/228550/8581231.html
http://paradise.ezla.com.tw/files/article/html/228/228549/8581228.html
http://paradise.ezla.com.tw/files/article/html/228/228550/8581235.html
http://paradise.ezla.com.tw/files/article/html/228/228550/8581235.html
http://paradise.ezla.com.tw/files/article/html/228/228551/8581238.html
http://paradise.ezla.com.tw/files/article/html/228/228550/8581242.html
http://paradise.ezla.com.tw/files/article/html/228/228551/8581243.html
http://paradise.ezla.com.tw/files/article/html/228/228549/8581245.html
http://paradise.ezla.com.tw/files/article/html/228/228551/8581298.html
http://paradise.ezla.com.tw/files/article/html/228/228551/8581299.html
http://paradise.ezla.com.tw/files/article/html/228/228549/8581301.html
http://paradise.ezla.com.tw/files/article/html/228/228550/8581306.html
http://paradise.ezla.com.tw/files/article/html/228/228549/8581303.html
http://paradise.ezla.com.tw/files/article/html/228/228549/8581307.html
http://paradise.ezla.com.tw/files/article/html/228/228549/8581303.html
http://paradise.ezla.com.tw/files/article/html/228/228551/8581308.html
http://paradise.ezla.com.tw/files/article/html/228/228551/8581309.html
http://paradise.ezla.com.tw/files/article/html/228/228549/8581310.html
http://paradise.ezla.com.tw/files/article/html/228/228549/8581315.html
http://paradise.ezla.com.tw/files/article/html/228/228551/8581316.html
http://paradise.ezla.com.tw/files/article/html/228/228550/8581318.html
http://paradise.ezla.com.tw/files/article/html/228/228550/8581323.html
http://paradise.ezla.com.tw/files/article/html/228/228550/8581323.html
http://paradise.ezla.com.tw/files/article/html/228/228551/8581324.html
http://paradise.ezla.com.tw/files/article/html/228/228549/8581325.html
http://paradise.ezla.com.tw/files/article/html/228/228551/8581329.html
http://paradise.ezla.com.tw/files/article/html/228/228550/8581331.html
http://paradise.ezla.com.tw/files/article/html/228/228549/8581332.html
http://paradise.ezla.com.tw/files/article/html/228/228551/8581334.html
http://paradise.ezla.com.tw/files/article/html/228/228550/8581336.html
http://paradise.ezla.com.tw/files/article/html/228/228551/8581337.html
http://paradise.ezla.com.tw/files/article/html/228/228549/8581339.html
http://paradise.ezla.com.tw/files/article/html/228/228550/8581338.html
http://paradise.ezla.com.tw/files/article/html/228/228549/8581342.html
http://paradise.ezla.com.tw/files/article/html/228/228549/8581342.html
http://paradise.ezla.com.tw/files/article/html/228/228551/8581343.html
http://paradise.ezla.com.tw/files/article/html/228/228550/8581344.html
http://paradise.ezla.com.tw/files/article/html/228/228550/8581347.html
http://paradise.ezla.com.tw/files/article/html/228/228549/8581348.html
http://paradise.ezla.com.tw/files/article/html/228/228551/8581349.html
http://paradise.ezla.com.tw/files/article/html/228/228549/8581350.html
http://paradise.ezla.com.tw/files/article/html/228/228550/8581352.html
http://paradise.ezla.com.tw/files/article/html/228/228550/8581353.html
http://paradise.ezla.com.tw/files/article/html/228/228551/8581354.html
http://paradise.ezla.com.tw/files/article/html/228/228549/8581355.html
http://paradise.ezla.com.tw/files/article/html/228/228551/8581356.html
http://paradise.ezla.com.tw/files/article/html/228/228551/8581360.html
http://paradise.ezla.com.tw/files/article/html/228/228550/8581362.html
http://paradise.ezla.com.tw/files/article/html/228/228549/8581365.html
http://paradise.ezla.com.tw/files/article/html/228/228551/8581367.html
http://paradise.ezla.com.tw/files/article/html/228/228551/8581369.html
http://paradise.ezla.com.tw/files/article/html/228/228551/8581370.html
http://paradise.ezla.com.tw/files/article/html/228/228549/8581373.html
http://paradise.ezla.com.tw/files/article/html/228/228549/8581374.html
http://paradise.ezla.com.tw/files/article/html/228/228551/8581375.html
http://paradise.ezla.com.tw/files/article/html/228/228549/8581379.html
http://paradise.ezla.com.tw/files/article/html/228/228549/8581377.html
http://paradise.ezla.com.tw/files/article/html/228/228550/8581376.html
http://paradise.ezla.com.tw/files/article/html/228/228551/8581381.html
http://paradise.ezla.com.tw/files/article/html/228/228551/8581382.html
http://paradise.ezla.com.tw/files/article/html/228/228549/8581383.html
http://paradise.ezla.com.tw/files/article/html/228/228549/8581384.html
http://paradise.ezla.com.tw/files/article/html/228/228551/8581388.html
http://paradise.ezla.com.tw/files/article/html/228/228551/8581391.html
http://paradise.ezla.com.tw/files/article/html/228/228550/8581390.html
http://paradise.ezla.com.tw/files/article/html/228/228550/8581393.html
http://paradise.ezla.com.tw/files/article/html/228/228551/8581394.html
http://paradise.ezla.com.tw/files/article/html/228/228550/8581398.html
http://paradise.ezla.com.tw/files/article/html/228/228550/8581400.html
http://paradise.ezla.com.tw/files/article/html/228/228551/8581401.html
http://paradise.ezla.com.tw/files/article/html/228/228550/8581397.html
http://paradise.ezla.com.tw/files/article/html/228/228551/8581407.html
http://paradise.ezla.com.tw/files/article/html/228/228550/8581405.html
http://paradise.ezla.com.tw/files/article/html/228/228550/8581409.html
http://paradise.ezla.com.tw/files/article/html/228/228551/8581412.html
http://paradise.ezla.com.tw/files/article/html/228/228549/8581413.html
http://paradise.ezla.com.tw/files/article/html/228/228551/8581414.html
http://paradise.ezla.com.tw/files/article/html/228/228549/8581415.html
http://paradise.ezla.com.tw/files/article/html/228/228549/8581416.html
http://paradise.ezla.com.tw/files/article/html/228/228549/8581417.html
http://paradise.ezla.com.tw/files/article/html/228/228549/8581421.html
http://paradise.ezla.com.tw/files/article/html/228/228550/8581423.html
http://paradise.ezla.com.tw/files/article/html/228/228551/8581424.html
http://paradise.ezla.com.tw/files/article/html/228/228551/8581422.html
http://paradise.ezla.com.tw/files/article/html/228/228549/8581426.html
http://paradise.ezla.com.tw/files/article/html/228/228551/8581427.html
http://paradise.ezla.com.tw/files/article/html/228/228550/8581425.html
http://paradise.ezla.com.tw/files/article/html/228/228551/8581429.html
http://paradise.ezla.com.tw/files/article/html/228/228552/8581302.html
http://paradise.ezla.com.tw/files/article/html/228/228552/8581302.html
http://paradise.ezla.com.tw/files/article/html/228/228553/8581304.html
http://paradise.ezla.com.tw/files/article/html/228/228552/8581311.html
http://paradise.ezla.com.tw/files/article/html/228/228552/8581313.html
http://paradise.ezla.com.tw/files/article/html/228/228552/8581313.html
http://paradise.ezla.com.tw/files/article/html/228/228553/8581314.html
http://paradise.ezla.com.tw/files/article/html/228/228552/8581317.html
http://paradise.ezla.com.tw/files/article/html/228/228554/8581321.html
http://paradise.ezla.com.tw/files/article/html/228/228552/8581319.html
http://paradise.ezla.com.tw/files/article/html/228/228554/8581321.html
http://paradise.ezla.com.tw/files/article/html/228/228553/8581326.html
http://paradise.ezla.com.tw/files/article/html/228/228554/8581328.html
http://paradise.ezla.com.tw/files/article/html/228/228554/8581328.html
http://paradise.ezla.com.tw/files/article/html/228/228552/8581330.html
http://paradise.ezla.com.tw/files/article/html/228/228554/8581333.html
http://paradise.ezla.com.tw/files/article/html/228/228552/8581335.html
http://paradise.ezla.com.tw/files/article/html/228/228553/8581341.html
http://paradise.ezla.com.tw/files/article/html/228/228552/8581345.html
http://paradise.ezla.com.tw/files/article/html/228/228552/8581346.html
http://paradise.ezla.com.tw/files/article/html/228/228552/8581351.html
http://paradise.ezla.com.tw/files/article/html/228/228553/8581357.html
http://paradise.ezla.com.tw/files/article/html/228/228552/8581358.html
http://paradise.ezla.com.tw/files/article/html/228/228552/8581359.html
http://paradise.ezla.com.tw/files/article/html/228/228553/8581361.html
http://paradise.ezla.com.tw/files/article/html/228/228554/8581364.html
http://paradise.ezla.com.tw/files/article/html/228/228554/8581364.html
http://paradise.ezla.com.tw/files/article/html/228/228552/8581366.html
http://paradise.ezla.com.tw/files/article/html/228/228554/8581368.html
http://paradise.ezla.com.tw/files/article/html/228/228553/8581371.html
http://paradise.ezla.com.tw/files/article/html/228/228554/8581372.html
http://paradise.ezla.com.tw/files/article/html/228/228554/8581378.html
http://paradise.ezla.com.tw/files/article/html/228/228554/8581380.html
http://paradise.ezla.com.tw/files/article/html/228/228552/8581385.html
http://paradise.ezla.com.tw/files/article/html/228/228554/8581386.html
http://paradise.ezla.com.tw/files/article/html/228/228554/8581387.html
http://paradise.ezla.com.tw/files/article/html/228/228554/8581389.html
http://paradise.ezla.com.tw/files/article/html/228/228552/8581392.html
http://paradise.ezla.com.tw/files/article/html/228/228552/8581395.html
http://paradise.ezla.com.tw/files/article/html/228/228554/8581396.html
http://paradise.ezla.com.tw/files/article/html/228/228554/8581399.html
http://paradise.ezla.com.tw/files/article/html/228/228553/8581402.html
http://paradise.ezla.com.tw/files/article/html/228/228553/8581403.html
http://paradise.ezla.com.tw/files/article/html/228/228554/8581404.html
http://paradise.ezla.com.tw/files/article/html/228/228552/8581406.html
http://paradise.ezla.com.tw/files/article/html/228/228552/8581410.html
http://paradise.ezla.com.tw/files/article/html/228/228554/8581408.html
http://paradise.ezla.com.tw/files/article/html/228/228553/8581411.html
http://paradise.ezla.com.tw/files/article/html/228/228554/8581418.html
http://paradise.ezla.com.tw/files/article/html/228/228553/8581419.html
http://paradise.ezla.com.tw/files/article/html/228/228553/8581419.html
http://paradise.ezla.com.tw/files/article/html/228/228552/8581247.html
http://paradise.ezla.com.tw/files/article/html/228/228553/8581248.html
http://paradise.ezla.com.tw/files/article/html/228/228553/8581250.html
http://paradise.ezla.com.tw/files/article/html/228/228554/8581253.html
http://paradise.ezla.com.tw/files/article/html/228/228554/8581254.html
http://paradise.ezla.com.tw/files/article/html/228/228552/8581255.html
http://paradise.ezla.com.tw/files/article/html/228/228553/8581256.html
http://paradise.ezla.com.tw/files/article/html/228/228552/8581260.html
http://paradise.ezla.com.tw/files/article/html/228/228552/8581263.html
http://paradise.ezla.com.tw/files/article/html/228/228553/8581267.html
http://paradise.ezla.com.tw/files/article/html/228/228552/8581271.html
http://paradise.ezla.com.tw/files/article/html/228/228554/8581273.html
http://paradise.ezla.com.tw/files/article/html/228/228554/8581275.html
http://paradise.ezla.com.tw/files/article/html/228/228552/8581277.html
http://paradise.ezla.com.tw/files/article/html/228/228552/8581277.html
http://paradise.ezla.com.tw/files/article/html/228/228553/8581276.html
http://paradise.ezla.com.tw/files/article/html/228/228554/8581279.html
http://paradise.ezla.com.tw/files/article/html/228/228553/8581285.html
http://paradise.ezla.com.tw/files/article/html/228/228552/8581287.html
http://paradise.ezla.com.tw/files/article/html/228/228552/8581289.html
http://paradise.ezla.com.tw/files/article/html/228/228554/8581290.html
http://paradise.ezla.com.tw/files/article/html/228/228553/8581297.html
http://paradise.ezla.com.tw/files/article/html/228/228550/8581246.html
http://paradise.ezla.com.tw/files/article/html/228/228551/8581249.html
http://paradise.ezla.com.tw/files/article/html/228/228551/8581251.html
http://paradise.ezla.com.tw/files/article/html/228/228550/8581252.html
http://paradise.ezla.com.tw/files/article/html/228/228550/8581261.html
http://paradise.ezla.com.tw/files/article/html/228/228550/8581261.html
http://paradise.ezla.com.tw/files/article/html/228/228549/8581259.html
http://paradise.ezla.com.tw/files/article/html/228/228550/8581262.html
http://paradise.ezla.com.tw/files/article/html/228/228550/8581261.html
http://paradise.ezla.com.tw/files/article/html/228/228551/8581264.html
http://paradise.ezla.com.tw/files/article/html/228/228550/8581265.html
http://paradise.ezla.com.tw/files/article/html/228/228549/8581266.html
http://paradise.ezla.com.tw/files/article/html/228/228551/8581268.html
http://paradise.ezla.com.tw/files/article/html/228/228550/8581269.html
http://paradise.ezla.com.tw/files/article/html/228/228550/8581270.html
http://paradise.ezla.com.tw/files/article/html/228/228551/8581272.html
http://paradise.ezla.com.tw/files/article/html/228/228550/8581278.html
http://paradise.ezla.com.tw/files/article/html/228/228551/8581282.html
http://paradise.ezla.com.tw/files/article/html/228/228551/8581282.html
http://paradise.ezla.com.tw/files/article/html/228/228549/8581281.html
http://paradise.ezla.com.tw/files/article/html/228/228550/8581283.html
http://paradise.ezla.com.tw/files/article/html/228/228550/8581284.html
http://paradise.ezla.com.tw/files/article/html/228/228549/8581288.html
http://paradise.ezla.com.tw/files/article/html/228/228549/8581288.html
http://paradise.ezla.com.tw/files/article/html/228/228549/8581291.html
http://paradise.ezla.com.tw/files/article/html/228/228551/8581292.html
http://paradise.ezla.com.tw/files/article/html/228/228551/8581293.html
http://paradise.ezla.com.tw/files/article/html/228/228550/8581295.html
http://paradise.ezla.com.tw/files/article/html/228/228550/8581295.html
http://paradise.ezla.com.tw/files/article/html/228/228550/8581296.html
http://paradise.ezla.com.tw/files/article/html/228/228551/8581432.html
http://paradise.ezla.com.tw/files/article/html/228/228549/8581434.html
http://paradise.ezla.com.tw/files/article/html/228/228550/8581435.html
http://paradise.ezla.com.tw/files/article/html/228/228549/8581439.html
http://paradise.ezla.com.tw/files/article/html/228/228549/8581444.html
http://paradise.ezla.com.tw/files/article/html/228/228550/8581445.html
http://paradise.ezla.com.tw/files/article/html/228/228551/8581449.html
http://paradise.ezla.com.tw/files/article/html/228/228550/8581450.html
http://paradise.ezla.com.tw/files/article/html/228/228551/8581453.html
http://paradise.ezla.com.tw/files/article/html/228/228550/8581459.html
http://paradise.ezla.com.tw/files/article/html/228/228549/8581456.html
http://paradise.ezla.com.tw/files/article/html/228/228550/8581459.html
http://paradise.ezla.com.tw/files/article/html/228/228549/8581461.html
http://paradise.ezla.com.tw/files/article/html/228/228551/8581465.html
http://paradise.ezla.com.tw/files/article/html/228/228550/8581464.html
http://paradise.ezla.com.tw/files/article/html/228/228550/8581464.html
http://paradise.ezla.com.tw/files/article/html/228/228550/8581466.html
http://paradise.ezla.com.tw/files/article/html/228/228551/8581467.html
http://paradise.ezla.com.tw/files/article/html/228/228550/8581468.html
http://paradise.ezla.com.tw/files/article/html/228/228551/8581470.html
http://paradise.ezla.com.tw/files/article/html/228/228551/8581470.html
http://paradise.ezla.com.tw/files/article/html/228/228550/8581471.html
http://paradise.ezla.com.tw/files/article/html/228/228549/8581475.html
http://paradise.ezla.com.tw/files/article/html/228/228549/8581479.html
http://paradise.ezla.com.tw/files/article/html/228/228550/8581480.html
http://paradise.ezla.com.tw/files/article/html/228/228549/8581485.html
http://paradise.ezla.com.tw/files/article/html/228/228550/8581486.html
http://paradise.ezla.com.tw/files/article/html/228/228551/8581487.html
http://paradise.ezla.com.tw/files/article/html/228/228549/8581488.html
http://paradise.ezla.com.tw/files/article/html/228/228549/8581490.html
http://paradise.ezla.com.tw/files/article/html/228/228551/8581491.html
http://paradise.ezla.com.tw/files/article/html/228/228550/8581492.html
http://paradise.ezla.com.tw/files/article/html/228/228549/8581494.html
http://paradise.ezla.com.tw/files/article/html/228/228549/8581497.html
http://paradise.ezla.com.tw/files/article/html/228/228550/8581498.html
http://paradise.ezla.com.tw/files/article/html/228/228551/8581502.html
http://paradise.ezla.com.tw/files/article/html/228/228551/8581503.html
http://paradise.ezla.com.tw/files/article/html/228/228549/8581504.html
http://paradise.ezla.com.tw/files/article/html/228/228550/8581505.html
http://paradise.ezla.com.tw/files/article/html/228/228550/8581511.html
http://paradise.ezla.com.tw/files/article/html/228/228550/8581511.html
http://paradise.ezla.com.tw/files/article/html/228/228549/8581514.html
http://paradise.ezla.com.tw/files/article/html/228/228551/8581515.html
http://paradise.ezla.com.tw/files/article/html/228/228551/8581510.html
http://paradise.ezla.com.tw/files/article/html/228/228549/8581518.html
http://paradise.ezla.com.tw/files/article/html/228/228550/8581521.html
http://paradise.ezla.com.tw/files/article/html/228/228549/8581523.html
http://paradise.ezla.com.tw/files/article/html/228/228549/8581525.html
http://paradise.ezla.com.tw/files/article/html/228/228551/8581526.html
http://paradise.ezla.com.tw/files/article/html/228/228549/8581529.html
http://paradise.ezla.com.tw/files/article/html/228/228549/8581529.html
http://paradise.ezla.com.tw/files/article/html/228/228550/8581531.html
http://paradise.ezla.com.tw/files/article/html/228/228550/8581534.html
http://paradise.ezla.com.tw/files/article/html/228/228551/8581537.html
http://paradise.ezla.com.tw/files/article/html/228/228549/8581538.html
http://paradise.ezla.com.tw/files/article/html/228/228551/8581542.html
http://paradise.ezla.com.tw/files/article/html/228/228549/8581545.html
http://paradise.ezla.com.tw/files/article/html/228/228549/8581545.html
http://paradise.ezla.com.tw/files/article/html/228/228551/8581546.html
http://paradise.ezla.com.tw/files/article/html/228/228550/8581548.html
http://paradise.ezla.com.tw/files/article/html/228/228549/8581552.html
http://paradise.ezla.com.tw/files/article/html/228/228550/8581549.html
http://paradise.ezla.com.tw/files/article/html/228/228550/8581553.html
http://paradise.ezla.com.tw/files/article/html/228/228549/8581557.html
http://paradise.ezla.com.tw/files/article/html/228/228549/8581561.html
http://paradise.ezla.com.tw/files/article/html/228/228549/8581562.html
http://paradise.ezla.com.tw/files/article/html/228/228550/8581563.html
http://paradise.ezla.com.tw/files/article/html/228/228549/8581565.html
http://paradise.ezla.com.tw/files/article/html/228/228549/8581567.html
http://paradise.ezla.com.tw/files/article/html/228/228551/8581569.html
http://paradise.ezla.com.tw/files/article/html/228/228550/8581571.html
http://paradise.ezla.com.tw/files/article/html/228/228551/8581573.html
http://paradise.ezla.com.tw/files/article/html/228/228551/8581574.html
http://paradise.ezla.com.tw/files/article/html/228/228549/8581576.html
http://paradise.ezla.com.tw/files/article/html/228/228549/8581579.html
http://paradise.ezla.com.tw/files/article/html/228/228550/8581577.html
http://paradise.ezla.com.tw/files/article/html/228/228549/8581579.html
http://paradise.ezla.com.tw/files/article/html/228/228550/8581580.html
http://paradise.ezla.com.tw/files/article/html/228/228549/8581583.html
http://paradise.ezla.com.tw/files/article/html/228/228550/8581584.html
http://paradise.ezla.com.tw/files/article/html/228/228549/8581585.html
http://paradise.ezla.com.tw/files/article/html/228/228549/8581587.html
http://paradise.ezla.com.tw/files/article/html/228/228550/8581586.html
http://paradise.ezla.com.tw/files/article/html/228/228551/8581594.html
http://paradise.ezla.com.tw/files/article/html/228/228549/8581593.html
http://paradise.ezla.com.tw/files/article/html/228/228549/8581595.html
http://paradise.ezla.com.tw/files/article/html/228/228551/8581596.html
http://paradise.ezla.com.tw/files/article/html/228/228551/8581597.html
http://paradise.ezla.com.tw/files/article/html/228/228549/8581598.html
http://paradise.ezla.com.tw/files/article/html/228/228550/8581603.html
http://paradise.ezla.com.tw/files/article/html/228/228553/8581428.html
http://paradise.ezla.com.tw/files/article/html/228/228552/8581431.html
http://paradise.ezla.com.tw/files/article/html/228/228554/8581430.html
http://paradise.ezla.com.tw/files/article/html/228/228554/8581433.html
http://paradise.ezla.com.tw/files/article/html/228/228552/8581436.html
http://paradise.ezla.com.tw/files/article/html/228/228553/8581438.html
http://paradise.ezla.com.tw/files/article/html/228/228552/8581437.html
http://paradise.ezla.com.tw/files/article/html/228/228552/8581442.html
http://paradise.ezla.com.tw/files/article/html/228/228552/8581442.html
http://paradise.ezla.com.tw/files/article/html/228/228554/8581440.html
http://paradise.ezla.com.tw/files/article/html/228/228553/8581443.html
http://paradise.ezla.com.tw/files/article/html/228/228553/8581446.html
http://paradise.ezla.com.tw/files/article/html/228/228554/8581447.html
http://paradise.ezla.com.tw/files/article/html/228/228552/8581448.html
http://paradise.ezla.com.tw/files/article/html/228/228554/8581451.html
http://paradise.ezla.com.tw/files/article/html/228/228553/8581452.html
http://paradise.ezla.com.tw/files/article/html/228/228554/8581457.html
http://paradise.ezla.com.tw/files/article/html/228/228553/8581460.html
http://paradise.ezla.com.tw/files/article/html/228/228554/8581457.html
http://paradise.ezla.com.tw/files/article/html/228/228553/8581460.html
http://paradise.ezla.com.tw/files/article/html/228/228553/8581462.html
http://paradise.ezla.com.tw/files/article/html/228/228553/8581472.html
http://paradise.ezla.com.tw/files/article/html/228/228553/8581473.html
http://paradise.ezla.com.tw/files/article/html/228/228552/8581474.html
http://paradise.ezla.com.tw/files/article/html/228/228554/8581476.html
http://paradise.ezla.com.tw/files/article/html/228/228554/8581477.html
http://paradise.ezla.com.tw/files/article/html/228/228552/8581478.html
http://paradise.ezla.com.tw/files/article/html/228/228554/8581481.html
http://paradise.ezla.com.tw/files/article/html/228/228553/8581482.html
http://paradise.ezla.com.tw/files/article/html/228/228552/8581483.html
http://paradise.ezla.com.tw/files/article/html/228/228553/8581484.html
http://paradise.ezla.com.tw/files/article/html/228/228554/8581489.html
http://paradise.ezla.com.tw/files/article/html/228/228553/8581493.html
http://paradise.ezla.com.tw/files/article/html/228/228552/8581495.html
http://paradise.ezla.com.tw/files/article/html/228/228553/8581496.html
http://paradise.ezla.com.tw/files/article/html/228/228553/8581500.html
http://paradise.ezla.com.tw/files/article/html/228/228553/8581499.html
http://paradise.ezla.com.tw/files/article/html/228/228554/8581501.html
http://paradise.ezla.com.tw/files/article/html/228/228554/8581506.html
http://paradise.ezla.com.tw/files/article/html/228/228552/8581512.html
http://paradise.ezla.com.tw/files/article/html/228/228553/8581513.html
http://paradise.ezla.com.tw/files/article/html/228/228552/8581512.html
http://paradise.ezla.com.tw/files/article/html/228/228553/8581513.html
http://paradise.ezla.com.tw/files/article/html/228/228554/8581516.html
http://paradise.ezla.com.tw/files/article/html/228/228553/8581517.html
http://paradise.ezla.com.tw/files/article/html/228/228553/8581519.html
http://paradise.ezla.com.tw/files/article/html/228/228554/8581520.html
http://paradise.ezla.com.tw/files/article/html/228/228552/8581522.html
http://paradise.ezla.com.tw/files/article/html/228/228552/8581524.html
http://paradise.ezla.com.tw/files/article/html/228/228554/8581527.html
http://paradise.ezla.com.tw/files/article/html/228/228553/8581530.html
http://paradise.ezla.com.tw/files/article/html/228/228552/8581532.html
http://paradise.ezla.com.tw/files/article/html/228/228554/8581535.html
http://paradise.ezla.com.tw/files/article/html/228/228554/8581535.html
http://paradise.ezla.com.tw/files/article/html/228/228553/8581536.html
http://paradise.ezla.com.tw/files/article/html/228/228553/8581539.html
http://paradise.ezla.com.tw/files/article/html/228/228553/8581541.html
http://paradise.ezla.com.tw/files/article/html/228/228554/8581543.html
http://paradise.ezla.com.tw/files/article/html/228/228552/8581540.html
http://paradise.ezla.com.tw/files/article/html/228/228553/8581547.html
http://paradise.ezla.com.tw/files/article/html/228/228552/8581550.html
http://paradise.ezla.com.tw/files/article/html/228/228553/8581551.html
http://paradise.ezla.com.tw/files/article/html/228/228554/8581554.html
http://paradise.ezla.com.tw/files/article/html/228/228552/8581555.html
http://paradise.ezla.com.tw/files/article/html/228/228554/8581556.html
http://paradise.ezla.com.tw/files/article/html/228/228554/8581558.html
http://paradise.ezla.com.tw/files/article/html/228/228553/8581559.html
http://paradise.ezla.com.tw/files/article/html/228/228553/8581560.html
http://paradise.ezla.com.tw/files/article/html/228/228552/8581564.html
http://paradise.ezla.com.tw/files/article/html/228/228554/8581566.html
http://paradise.ezla.com.tw/files/article/html/228/228552/8581568.html
http://paradise.ezla.com.tw/files/article/html/228/228554/8581570.html
http://paradise.ezla.com.tw/files/article/html/228/228553/8581572.html
http://paradise.ezla.com.tw/files/article/html/228/228552/8581575.html
http://paradise.ezla.com.tw/files/article/html/228/228552/8581581.html
http://paradise.ezla.com.tw/files/article/html/228/228553/8581582.html配 "food" 中的两个 o。
{n,} n 是一个非负整数。至少匹配 n 次。例如,'o{2,}' 不能匹配 "Bob" 中的 'o',但能匹配 "foooood" 中的所有 o。'o{1,}' 等价于 'o+'。'o{0,}' 则等价于 'o*'。
{n,m} m 和 n 均为非负整数,其中n <= m。最少匹配 n 次且最多匹配 m 次。例如,"o{1,3}" 将匹配 "fooooood" 中的前三个 o。'o{0,1}' 等价于 'o?'。请注意在逗号和两个数之间不能有空格。
除了元字符之外,用户还可以精确指定模式在匹配对象中出现的频率。例如,/jim {2,6}/ 上述正则表达式规定字符m可以在匹配对象中连续出现2-6次,因此,上述正则表达式可以同jimmy或jimmmmmy等字符串相匹配。
在对如何使用正则表达式有了初步了解之后,我们来看一下其它几个重要的元字符的使用方式。
\s:用于匹配单个空格符,包括tab键和换行符;
\S:用于匹配除单个空格符之外的所有字符;
\d:用于匹配从0到9的数字;
\w:用于匹配字母,数字或下划线字符;
\W:用于匹配所有与\w不匹配的字符;
. :用于匹配除换行符之外的所有字符。
(说明:我们可以把\s和\S以及\w和\W看作互为逆运算)
下面,我们就通过实例看一下如何在正则表达式中使用上述元字符。
/\s+/ 上述正则表达式可以用于匹配目标对象中的一个或多个空格字符。
/\d000/ 如果我们手中有一份复杂的财务报表,那么我们可以通过上述正则表达式轻而易举的查找到所有总额达千元的款项。
除了我们以上所介绍的元字符之外,正则表达式中还具有另外一种较为独特的专用字符,即定位符。定位符用于规定匹配模式在目标对象中的出现位置。 较为常用的定位符包括: “^”, “$”, “\b” 以及 “\B”。
“^”定位符规定匹配模式必须出现在目标字符串的开头
“$”定位符规定匹配模式必须出现在目标对象的结尾
“\b”定位符规定匹配模式必须出现在目标字符串的开头或结尾的两个边界之一
“\B”定位符则规定匹配对象必须位于目标字符串的开头和结尾两个边界之内,
即匹配对象既不能作为目标字符串的开头,也不能作为目标字符串的结尾。
同样,我们也可以把“^”和“$”以及“\b”和“\B”看作是互为逆运算的两组定位符。举例来说: /^hell/ 因为上述正则表达式中包含“^”定位符,所以可以与目标对象中以 “hell”, “hello”或“hellhound”开头的字符串相匹配。 /ar$/ 因为上述正则表达式中包含“$”定位符,所以可以与目标对象中以 “car”, “bar”或 “ar” 结尾的字符串相匹配。 /\bbom/ 因为上述正则表达式模式以“\b”定位符开头,所以可以与目标对象中以 “bomb”, 或 “bom”开头的字符串相匹配。/man\b/ 因为上述正则表达式模式以“\b”定位符结尾,所以可以与目标对象中以 “human”, “woman”或 “man”结尾的字符串相匹配。
为了能够方便用户更加灵活的设定匹配模式,正则表达式允许使用者在匹配模式中指定某一个范围而不局限于具体的字符。例如:
/[A-Z]/ 上述正则表达式将会与从A到Z范围内任何一个大写字母相匹配。
/[a-z]/ 上述正则表达式将会与从a到z范围内任何一个小写字母相匹配。
/[0-9]/ 上述正则表达式将会与从0到9范围内任何一个数字相匹配。
/([a-z][A-Z][0-9])+/ 上述正则表达式将会与任何由字母和数字组成的字符串,如 “aB0” 等相匹配。
这里需要提醒用户注意的一点就是可以在正则表达式中使用 “()” 把字符串组合在一起。“()”符号包含的内容必须同时出现在目标对象中。因此,上述正则表达式将无法与诸如 “abc”等的字符串匹配,因为“abc”中的最后一个字符为字母而非数字。
如果我们希望在正则表达式中实现类似编程逻辑中的“或”运算,在多个不同的模式中任选一个进行匹配的话,可以使用管道符 “|”。例如:/to|too|2/ 上述正则表达式将会与目标对象中的 “to”, “too”, 或 “2” 相匹配。
正则表达式中还有一个较为常用的运算符,即否定符 “[^]”。与我们前文所介绍的定位符 “^” 不同,否定符 “[^]”规定目标对象中不能存在模式中所规定的字符串。例如:/[^A-C]/ 上述字符串将会与目标对象中除A,B,和C之外的任何字符相匹配。一般来说,当“^”出现在 “[]”内时就被视做否定运算符;而当“^”位于“[]”之外,或没有“[]”时,则应当被视做定位符。
最后,当用户需要在正则表达式的模式中加入元字符,并查找其匹配对象时,可以使用转义符“\”。例如:/Th\*/ 上述正则表达式将会与目标对象中的“Th*”而非“The”等相匹配。
在构造正则表达式之后,就可以象数学表达式一样来求值,也就是说,可以从左至右并按照一个优先级顺序来求值。优先级如下:
1.\ 转义符
2.(), (?:), (?=), [] 圆括号和方括号
3.*, +, ?, {n}, {n,}, {n,m} 限定符
4.^, $, \anymetacharacter 位置和顺序
5.|“或”操作
使用实例
在JavaScript 1.2中带有一个功能强大的RegExp()对象,可以用来进行正则表达式的匹配操作。其中的test()方法可以检验目标对象中是否包含匹配模式,并相应的返回true或false。
我们可以使用JavaScript编写以下脚本,验证用户输入的邮件地址的有效性。
正则表达式对象
本对象包含正则表达式模式以及表明如何应用模式的标志。
语法 1 re = /pattern/[flags]
语法 2 re = newRegExp("pattern",["flags"])
参数
re
必选项。将要赋值为正则表达式模式的变量名。
Pattern
必选项。要使用的正则表达式模式。如果使用语法 1,用 "/" 字符分隔模式。如果用语法 2,用引号将模式引起来。
Flags
可选项。如果使用语法 2 要用引号将 flag 引起来。标志可以组合使用,可用的有:
g (全文查找出现的所有 pattern)
i (忽略大小写)
m (多行查找)
示例
下面的示例创建一个包含正则表达式模式及相关标志的对象(re),向您演示正则表达式对象的用法。在本例中,作为结果的正则表达式对象又用于 match 方法中:
function MatchDemo()
{
var r, re; // 声明变量。
var s = "The rain in Spain falls mainly in theplain";
re = new RegExp("ain","g"); // 创建正则表达式对象。
r = s.match(re); // 在字符串 s 中查找匹配。
return(r);
}
js中exec、test、match、search、replace、split用法
exec:对string进行正则处理,并返回匹配结果.array[0]为原字符串,array[i]为匹配在整个被搜索字符串中的位置。
test:测试string是否包含有匹配结果,包含返回true,不包含返回false。
match(pattern) :根据pattern进行正则匹配,如果匹配到,返回匹配结果,如匹配不到返回null
search(pattern) :根据pattern进行正则匹配,如果匹配到一个结果,则返回它的索引数;否则返回-1
replace(pattern,replacement) :根据pattern进行正则匹配,把匹配结果替换为replacement
split(pattern) :根据pattern进行正则分割,返回一个分割的数组
--------------------------------------------------------------------------------------
match 方法
使用正则表达式模式对字符串执行查找,并将包含查找的结果作为数组返回。
stringObj.match(rgExp)
参数
stringObj
必选项。对其进行查找的 String 对象或字符串文字。
rgExp
必选项。为包含正则表达式模式和可用标志的正则表达式对象。也可以是包含正则表达式模式和可用标志的变量名或字符串文字。
其余说明与exec一样,不同的是如果match的表达式匹配了全局标记g将出现所有匹配项,而不用循环,但所有匹配中不会包含子匹配项。
例子1:
function MatchDemo(){
var r, re; // 声明变量。
var s = "The rain in Spain falls mainly in the plain";
re = /(a)in/ig; // 创建正则表达式模式。
r = s.match(re); // 尝试去匹配搜索字符串。
document.write(r); // 返回的数组包含了所有 "ain" 出现的四个匹配,r[0]、r[1]、r[2]、r[3]。 // 但没有子匹配项a。
}
输出结果:ain,ain,ain,ain
exec 方法
用正则表达式模式在字符串中查找,并返回该查找结果的第一个值(数组),如果匹配失败,返回null。
rgExp.exec(str)
参数
rgExp
必选项。包含正则表达式模式和可用标志的正则表达式对象。
str
必选项。要在其中执行查找的 String 对象或字符串文字。
返回数组包含:
input:整个被查找的字符串的值;
index:匹配结果所在的位置(位);
lastInput:下一次匹配结果的位置;
arr:结果值,arr[0]全匹配结果,arr[1,2...]为表达式内()的子匹配,由左至右为1,2...。
例子2:
function RegExpTest(){
var src="http://sumsung753.blog.163.com/blog/I love you!";
var re = /\w+/g; // 注意g将全文匹配,不加将永远只返回第一个匹配。
var arr;
while((arr = re.exec(src)) !=null){ //exec使arr返回匹配的第一个,while循环一次将使re在g作用寻找下一个匹配。
document.write(arr.index + "-" + arr.lastIndex + ":" + arr + "<br/>");
for(key in arr){
document.write(key + "=>" + arr[key] + "<br/>");
}
document.write("<br/>");
}
}
window.onload = RegExpTest();
输出结果:
0-1:I //0为index,i所在位置,1为下一个匹配所在位置
input=>I love you!
index=>0
lastIndex=>1
0=>I
2-6:love
input=>I love you!
index=>2
lastIndex=>6
0=>love
7-10:you
input=>I love you!
index=>7
lastIndex=>10
0=>you
说明:根据手册,exec只返回匹配结果的第一个值,比如上例如果不用while循环,将只返回'I'(尽管i空格后的love和you都符合表达式),无论re表达式用不用全局标记g。但是如果为正则表达式设置了全局标记g,exec 从以 lastIndex 的值指示的位置开始查找。如果没有设置全局标志,exec 忽略 lastIndex 的值,从字符串的起始位置开始搜索。利用这个特点可以反复调用exec遍历所有匹配,等价于match具有g标志。
当然,如果正则表达式忘记用g,而又用循环(比如:while、for等),exec将每次都循环第一个,造成死循环。
exec的输出将包含子匹配项。
例子3:
function execDemo(){ |
var r, re; // 声明变量。
var s = "The rain in Spain falls mainly in the plain";
re = /[\w]*(ai)n/ig;
r = re.exec(s);
document.write(r + "<br/>");
for(key in r){
document.write(key + "-" + r[key] + "<br/>");
}
}
window.onload = execDemo();
输出:
rain,ai
input-The rain in Spain falls mainly in the plain
index-4
lastIndex-8
0-rain
1-ai
test 方法
返回一个 Boolean 值,它指出在被查找的字符串中是否匹配给出的正则表达式。
rgexp.test(str)
参数
rgexp
必选项。包含正则表达式模式或可用标志的正则表达式对象。
str
必选项。要在其上测试查找的字符串。
说明
test 方法检查字符串是否与给出的正则表达式模式相匹配,如果是则返回 true,否则就返回 false。
例子4:
function TestDemo(re, s){
var s1;
if (re.test(s))
s1 = " 匹配正则式 ";
else
s1 = " 不匹配正则式 ";
return("'" + s + "'" + s1 + "'"+ re.source + "'");
}
window.onload = document.write(TestDemo(/ab/,'cdef'));
输出结果:'cdef' 不匹配正则式 'ab'
注意:test()继承正则表达式的lastIndex属性,表达式在匹配全局标志g的时候须注意。
例子5:
function testDemo(){
var r, re; // 声明变量。
var s = "I";
re = /I/ig; // 创建正则表达式模式。
document.write(re.test(s) + "<br/>"); // 返回 Boolean 结果。
document.write(re.test(s) + "<br/>");
document.write(re.test(s));
}
testDemo();
输出结果:
true
false
true
当第二次调用test()的时候,lastIndex指向下一次匹配所在位置1,所以第二次匹配不成功,lastIndex重新指向0,等于第三次又重新匹配。下例显示test的lastIndex属性:
例子6:
function testDemo(){
var r, re; // 声明变量。
var s = "I";
re = /I/ig; // 创建正则表达式模式。
document.write(re.test(s) + "<br/>"); // 返回 Boolean 结果。
document.write(re.lastIndex); // 返回 Boolean 结果。
}
testDemo();
输出:
true
1
解决方法:将test()的lastIndex属性每次重新指向0,re.lastIndex = 0;
search 方法
返回与正则表达式查找内容匹配的第一个子字符串的位置(偏移位)。
stringObj.search(rgExp)
参数
stringObj
必选项。要在其上进行查找的 String 对象或字符串文字。
rgExp
必选项。包含正则表达式模式和可用标志的正则表达式对象。
说明:如果找到则返回子字符至开始处的偏移位,否则返回-1。
例子7:
function SearchDemo(){
var r, re; // 声明变量。
var s = "The rain in Spain falls mainly in the plain.";
re = /falls/i; // 创建正则表达式模式。
re2 = /tom/i;
r = s.search(re); // 查找字符串。
r2 = s.search(re2);
return("r:" + r + ";r2:" + r2); // 返回 Boolean 结果。
}
document.write(SearchDemo());
输出:r:18;r2:-1
replace 方法
返回根据正则表达式进行文字替换后的字符串的复制。
stringObj.replace(rgExp, replaceText)
参数
stringObj
必选项。要执行该替换的 String 对象或字符串文字。该字符串不会被 replace 方法修改。
rgExp
必选项。为包含正则表达式模式或可用标志的正则表达式对象。也可以是 String 对象或文字。如果 rgExp 不是正则表达式对象,它将被转换为字符串,并进行精确的查找;不要尝试将字符串转化为正则表达式。
replaceText
必选项。是一个String 对象或字符串文字,对于stringObj 中每个匹配 rgExp 中的位置都用该对象所包含的文字加以替换。在 Jscript 5.5 或更新版本中,replaceText 参数也可以是返回替换文本的函数。
说明 :replace 方法的结果是一个完成了指定替换的 stringObj 对象的复制。意思为匹配的项进行指定替换,其它不变作为StringObj的原样返回。
ECMAScript v3 规定,replace() 方法的参数 replacement 可以是函数而不是字符串。在这种情况下,每个匹配都调用该函数,它返回的字符串将作为替换文本使用。该函数的第一个参数是匹配模式的字符串。接下来的参数是与模式中的子表达式匹配的字符串,可以有 0 个或多个这样的参数。接下来的参数是一个整数,声明了匹配在 stringObject 中出现的位置。最后一个参数是 stringObject 本身。结果为将每一匹配的子字符串替换为函数调用的相应返回值的字符串值。函数作参可以进行更为复杂的操作。
例子8:
function f2c(s) {
var test = /(\d+(\.\d*)?)F\b/g; // 说明华氏温度可能模式有:123F或123.4F。注意,这里用了g模式
return(s.replace
(test,
function(Regstr,$1,$2,$3,newstrObj) {
return(("<br/>" + Regstr +"<br/>" + ($1-32) * 1/2) + "C" +"<br/>" + //以下两行进行替换
$2 +"<br/>" + $3 +"<br/>" + newstrObj +"<br/>" );
}
)
);
}
document.write(f2c("Water: 32.2F and Oil: 20.30F."));
输出结果:
Water: //不与正则匹配的字符,按原字符输出
32.2F //与正则相匹配的第一个字符串的原字符串 Regstr
0.10000000000000142C //与正则相匹配的第一个字符串的第一个子模式匹配的替换结果 $1
.2 //与正则相匹配的第一个字符串的第二个子模式匹配项的替换结果,这里我们没有将它替换 $2
7 //与正则相匹配的第一个字符串的第一个子匹配出现的偏移量 $3
Water: 32.2F and Oil: 20.30F. //原字符串 newstrObj
and Oil: //不与正则匹配的字符
20.30F //与正则相匹配的第二个字符串的原字符串
-5.85C //与正则相匹配的第二个字符串的第一个子模式与匹配的替换结果
.30 //与正则相匹配的第二个字符串的第二个子模式匹配项的替换结果,这里我们没有将它替换
22 //与正则相匹配的第二个字符串的第一个子匹配出现的偏移量
Water: 32.2F and Oil: 20.30F. //原字符串
. //不与正则匹配的字符
上面的函数参数我们全部用到了。在实际中,我们只须用将xxF替换为xxC,根据要求,我们无须写这么多参数。
例子9:
function f2c(s) {
var test = /(\d+(\.\d*)?)F\b/g; // 说明华氏温度可能模式有:123F或123.4F
return(s.replace
(test,
function(strObj,$1) {
return((($1-32) * 1/2) + "C");
}
)
);
}
document.write(f2c("Water: 32.2F and Oil: 20.30F."));
输出:Water: 0.10000000000000142C and Oil: -5.85C.
更多的应用:
例子10:
function f2c(s) {
var test = /([\d]{4})-([\d]{1,2})-([\d]{1,2})/;
return(s.replace
(test,
function($0,$1,$2,$3) {
return($2 +"/" + $1);
}
)
);
}
document.write(f2c("today: 2011-03-29"));
输出:today: 03/2011
split 方法
将一个字符串分割为子字符串,然后将结果作为字符串数组返回。
stringObj.split([separator[, limit]])
参数
stringObj
必选项。要被分解的 String 对象或文字。该对象不会被 split 方法修改。
separator
可选项。字符串或 正则表达式 对象,它标识了分隔字符串时使用的是一个还是多个字符。如果忽略该选项,返回包含整个字符串的单一元素数组。
limit
可选项。该值用来限制返回数组中的元素个数。
说明 :split 方法的结果是一个字符串数组,在 stingObj 中每个出现 separator 的位置都要进行分解。separator 不作为任何数组元素的部分返回。
例子11:
function SplitDemo(){
var s, ss;
var s = "The rain in Spain falls mainly in the plain.";
// 正则表达式,用不分大不写的s进行分隔。
ss = s.split(/s/i);
return(ss);
}
document.write(SplitDemo());
输出:The rain in ,pain fall, mainly in the plain.