Article Directory
- 1. Give a concatenated string such as: get-element-by-id into camel case.
- 2. Match binary numbers
- 3. Non-zero decimal number (has at least one digit, but cannot start with 0)
- 4. Match 12 months of the year
- 5. The longest matching qq number is 13
- 6. Match common fixed phone numbers
- 7. Match ip address
- 8. Match the string beginning with a enclosed in angle brackets
- 9. Divide every three numbers with a comma
- 10. Determine whether the string contains numbers
- 11. Determine the phone number
- 12. Determine whether it meets the specified format
- 13. Determine whether it conforms to the USD format
- 14. JS implements thousands separator
- 15. Get url parameters
- 16.Verify email
- 17. Verify ID number
- 18.Match Chinese characters
- 19. Remove the leading and trailing'/'
- 20. Judge whether the date format meets the form of '2017-05-11', simply judge, only judge the format
- 21. Judge whether the date format conforms to the form of '2017-05-11', strictly judge (more complicated)
- 22. IPv4 address regular
- 23. Hexadecimal color regular
- 24. License plate number regular
- 25. Filter HTML tags
- 26. Regular password strength, at least 6 characters, including at least 1 uppercase letter, 1 lowercase letter, 1 number, and 1 special character
- 27. URL regular
- 28. Match floating point numbers
1. Give a concatenated string such as: get-element-by-id into camel case.
var str = "get-element-by-id";
var reg = /-\w/g; // 匹配横杆以及之后的一个字符,全局匹配
console.log(str.replace(reg,function($0){
return $0.slice(1).toUpperCase();
// 匹配到到是-e -b -i 形式截取后一个字符转成大写
}));
2. Match binary numbers
var str = "10101111";
var reg = /^[01]+$/g;
console.log(reg.test(str));
3. Non-zero decimal number (has at least one digit, but cannot start with 0)
var str = "81";
var reg = /^[1-9][0-9]?$/g;
console.log(reg.test(str));
4. Match 12 months of the year
var str = "12";
var reg = /^(0?[1-9]|1[0-2])$/g;
console.log(reg.test(str));
5. The longest matching qq number is 13
var str ="10009093283333";
var reg = /^[1-9][0-9]{4,12}$/g;
console.log(reg.test(str));
6. Match common fixed phone numbers
var str = "000-12344562";
// \(? 匹配左括号一次或0次然后以0开头后面加两个数字,再匹配右括号或空格或减号一次或0次,随后匹配8个数字
var reg = /\(?0\d{2}[) -]?\d{8}/g;
console.log(str.match(reg));
7. Match ip address
var str = "255.221.221.12";
// [01]?\d\d?表示匹配小于199的数,可以说两位数或一位数,2[0-4]\d表示从200到249,配合25[0-5]就表示小于255的数了。
var reg = /(([01]?\d\d?|2[0-4]\d|25[0-5])\.){3}([01]?\d\d?|2[0-4]\d|25[0-5])/g;
console.log(str.match(reg));
8. Match the string beginning with a enclosed in angle brackets
var str = "<a herf='www.baidu.com'>";
var reg = /<a[^>]+>/g;
console.log(str.match(reg));
9. Divide every three numbers with a comma
var str = "12345678901";
function numSplit(str){
var re = /(\d)(?=(\d{3})+$)/g;
//(\d{3})+$ 的意思是连续匹配 3 个数字,且最后一次匹配以 3 个数字结尾。
//要找到所有的单个字符,这些字符的后面跟随的字符的个数必须是3的倍数,并在符合条件的单个字符后面添加,
return str.replace(re,'$1,');
}
console.log(numSplit(str));
10. Determine whether the string contains numbers
function containsNumber(str) {
var regx = /\d/;
return regx.text(str);
}
11. Determine the phone number
function isPhone(tel) {
var regx = /^1[34578]\d{9}$/;
return regx.test(tel);
}
12. Determine whether it meets the specified format
Given string str, check whether it conforms to the following format
- XXX-XXX-XXXX
- Where X is Number type
function matchesPattern(str) {
return /^(\d{
3}-){
2}\d{
4}&/.test(str);
}
13. Determine whether it conforms to the USD format
Given a string str, check whether it conforms to the US dollar writing format
- Start with $
- Integer part, starting from the ones digit, separated by 3 digits
- If it is a decimal, the length of the decimal part is 2
- The correct format is: $1,023,032.03 or $2.03, the wrong format is: $3,432,12.12 or $34,344.3**
function isUSD(str) {
var regx = /^\$\d{1,3}(,\d{3})*(\.\d{2})?$/;
return regx.test(str);
}
14. JS implements thousands separator
function format(number) {
var regx = /\d{1,3}(?=(\d{3})+$)/g;
return (number + '').replace(regx, '$&,') // $&表示与regx相匹配的字符串
}
15. Get url parameters
Get the parameters in the url
- Specify the parameter name, return the value of the parameter or an empty string
- Without specifying the parameter name, return all parameter objects or {}
- If there are multiple parameters with the same name, return an array
function getUrlParam(url, key) {
var arr = {
};
url.replace(/\??(\w+)=(\w+)&?/g, function(match, matchKey, matchValue) {
if (!arr[matchKey]) {
arr[matchKey] = matchValue;
} else {
var temp = arr[matchKey];
arr[matchKey] = [].concat(temp, matchValue);
}
});
if (!key) {
return arr;
} else {
for (ele in arr) {
if (ele = key) {
return arr[ele];
}
}
return '';
}
}
16.Verify email
function isEmail(email) {
var regx = /^([a-zA-Z0-9_\-])+@([a-zA-Z0-9_\-])+(\.[a-zA-Z0-9_\-])+$/;
return regx.test(email);
}
17. Verify ID number
The ID number may be 15 or 18 digits, 15 digits are all numbers, the first 17 digits of the 18 digits are numbers, and the last digit is a number or X
function isCardNo(number) {
var regx = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
return regx.test(number);
}
18.Match Chinese characters
var regx = /^[\u4e00-\u9fa5]{0,}$/;
19. Remove the leading and trailing'/'
var str = '/asdf//';
str = str.replace(/^\/*|\/*$/g, '');
20. Judge whether the date format meets the form of '2017-05-11', simply judge, only judge the format
var regx = /^\d{4}\-\d{1,2}\-\d{1,2}$/
21. Judge whether the date format conforms to the form of '2017-05-11', strictly judge (more complicated)
var regx = /^(?:(?!0000)[0-9]{4}-(?:(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-8])|(?:0[13-9]|1[0-2])-(?:29|30)|(?:0[13578]|1[02])-31)|(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)-02-29)$/;
22. IPv4 address regular
var regx = /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;
23. Hexadecimal color regular
var regx = /^#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$/;
24. License plate number regular
var regx = /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-Z0-9]{4}[A-Z0-9挂学警港澳]{1}$/;
25. Filter HTML tags
var str="<p>dasdsa</p>nice <br> test</br>"
var regx = /<[^<>]+>/g;
str = str.replace(regx, '');
26. Regular password strength, at least 6 characters, including at least 1 uppercase letter, 1 lowercase letter, 1 number, and 1 special character
var regx = /^.*(?=.{6,})(?=.*\d)(?=.*[A-Z])(?=.*[a-z])(?=.*[!@#$%^&*? ]).*$/;
27. URL regular
var regx = /^((https?|ftp|file):\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/;
28. Match floating point numbers
var regx = /^-?([1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0)$/;