<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<script type="text/javascript">
/*正则表达式中\作为转义字符
* .表示任意字符,\.来检查含.
* 注意,使用构造函数时,由于它的参数是一个字符串,
* 如果需要使用\,要写\ */
//.\ 制表符
// /\b/ 匹配单词边界
// /\B/ 不匹配单词边界
// ...
// https://www.w3cschool.cn/jsref/jsref-obj-regexp.html
//.检查字符串中是否有单词child
var reg=/child/;
var reg1=/\bchild\b/; //单词边界,注册用户名不允许空格常用
console.log(reg.test("Hello child")); //true
console.log(reg.test("Hello children"));//true,但是不符合题意
console.log(reg1.test("Hello children"));//false
//.去空格,用户输入用户名会在前后不小心打入空格,智能去除
var str=" Hello W ";
//前后全部空格 /^\s*/ /\s*$/
str=str.replace(/^\s*|\s*$/g,"");
console.log(str);//Hello W
</script>
</body>
</html>
原文:https://www.cnblogs.com/wangdongwei/p/11251438.html