1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <meta name="viewport" content="width=device-width, initial-scale=1.0"> 6 <title>Document</title> 7 <script> 8 /* 验证 */ 9 //定义一个正则表达式 10 var reg = /o/ 11 //自定义一个字符串 12 var str = 'hello world!' 13 //校验是否符合正则的规则 14 console.log(reg.test(str)) 15 16 /* 匹配 */ 17 var reg = /o/g //g是全局的意思,不带g只匹配第一个 18 var str = 'hello world!' 19 var result = str.match(reg) 20 console.log(result) 21 22 /* 替换 */ 23 var reg = /o/gi //i是忽略大小写 24 var str = 'hello wOrld!' 25 var newstr = str.replace(reg,'@') 26 27 /* 验证 */ 28 var str1 = 'java love me' 29 var str2 = 'I love java' 30 var reg1 = /^java/ //测试是否以java开头 31 var reg2 = /java$/ //测试是否以java结尾 32 console.log(reg1.test(str1)) 33 console.log(reg2.test(str2)) 34 35 /* 验证 */ 36 //必须是字母开头,长度必须是6-10位,后面其他字符可以是大小写字母、数字、_ 37 var str = '' 38 var reg = /^[a-zA-Z][a-zA-Z0-9_]{5,9}$/ 39 console.log(reg.test(str)) 40 </script> 41 </head> 42 <body> 43 44 </body> 45 </html>
标签:java,log,16,正则表达式,var,str,字符串,console,reg From: https://www.cnblogs.com/IrVolcano/p/18100165