JS中test match replace的三个方法的用法区别

 

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <meta http-equiv="X-UA-Compatible" content="ie=edge" />
  <title>Document</title>
</head>

<body>

</body>
<script>
  let isNum = /^\d*$/g  // 校验是否为数字,
  let stringData = '123'
  // regex.test(String)
  console.log("TCL:  isNum.test(stringData)", isNum.test(stringData))  // true or false
  // String.match(regex)
  console.log("TCL: stringData.match(isNum)", stringData.match(isNum))  // 数组 or null
  // String.replace(regex,'*')
  console.log("TCL: stringData.replace(isNum,'*')", stringData.replace(isNum, '*'))  //  *

  var str = 'abc/de/f'
  var regex = /[/]/g
  str.replace(regex, '-')
  console.log("TCL: str.replace(regex, '-')", str.replace(regex, '-'))  // abc-de-f

</script>

</html>
发布了171 篇原创文章 · 获赞 499 · 访问量 11万+

猜你喜欢

转载自blog.csdn.net/qq_43258252/article/details/103353122