Home  >  Article  >  Web Front-end  >  How to find letters and numbers in js using regular expressions

How to find letters and numbers in js using regular expressions

php中世界最好的语言
php中世界最好的语言Original
2018-03-29 17:51:231814browse

This time I will bring you the use of regular expressionsHow to find letters and numbers in js, and what are the things to note when using regular expressions to find letters and numbers in js , the following is a practical case, let’s take a look.

Without further ado, let’s take a look at how to use regular expressions to find letters and numbers in JS. The specific code is as follows:

<!DOCTYPE HTML>
<html >
<head>
<meta charset="UTF-8">
<title></title>
<script type="text/javascript">
window.onload=function(){
localStorage.fiveData="你好啊 世界";
//alert(localStorage.fiveData);
//var reg=/\d+/g;//匹配任意长度数字
var reg=/[a-z,A-Z]/g;//匹配任意字母
var str="aajdaf1232jadlfjakdjfalkjlakfj2131l34kalfsjaafaqejqe231wk";
var t=str.match(reg);
for(var c in t){
console.log(t[c]);
}
}
</script>
</head>
<body>
</body>
</html>

Let’s take a look at using regular expressions in JS Expression, method to verify that the password contains numbers and letters

must contain at least one number and one letter, the script method is as follows:

function CheckPassWord(password) {//密码必须包含数字和字母
  var str = password;
  if (str == null || str.length < 8) {
    return false;
  }
  var reg = new RegExp(/^(?![^a-zA-Z]+$)(?!\D+$)/);
  if (reg.test(str))
    return true;
}
must contain numbers plus letters It cannot contain special symbols, etc. The script method is as follows:

function CheckPassWord(password) {//必须为字母加数字且长度不小于8位
  var str = password;
  if (str == null || str.length <8) {
    return false;
  }
  var reg1 = new RegExp(/^[0-9A-Za-z]+$/);
  if (!reg1.test(str)) {
    return false;
  }
  var reg = new RegExp(/[A-Za-z].*[0-9]|[0-9].*[A-Za-z]/);
  if (reg.test(str)) {
    return true;
  } else {
    return false;
  }
}
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the PHP Chinese website!

Recommended reading:

How to use regular expressions to implement the verification code of page forms

How to use regular expressions in PHP and JS Match Chinese characters

The above is the detailed content of How to find letters and numbers in js using regular expressions. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn