1. var s1 = "get-element-by-id"; Given such a concatenated string, write a function to convert the string getElementById into camel case notation
var f = function(s) {
return s.replace(/-\w/g, function(x) {
return x.slice(1).toUpperCase();
})
}
2. Determine whether the string contains numbers
function containsNumber(str) {
var regx = /\d/;
return regx.text(str);
}
3. Determine the phone number
function isPhone(tel) {
var regx = /^1[34578]\d{9}$/;
return regx.test(tel);
}
4. Determine whether it conforms to the specified format
Given a string str, check if it conforms to the following format
- XXX-XXX-XXXX
- where X is of type Number
function matchesPattern(str) {
return /^(\d{3}-){2}\d{4}&/.test(str);
}
5. Determine whether it conforms to the USD format
Given a string str, check if it conforms to dollar-writing format
- starts with $
- Integer part, starting from the one digit, separated by 3 digits
- If decimal, the fractional part length 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);
}
6. JS implements thousands separator
function format(number) {
var regx = /\d{1,3}(?=(\d{3})+$)/g;
return (number + '').replace(regx, '$&,') // $&表示与regx相匹配的字符串
}
7. Get url parameters
Get the parameters in the url
- Specify the parameter name, return the value of the parameter or an empty string
- If no parameter name is specified, return all parameter objects or {}
- Returns an array if there are multiple parameters with the same name
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 '';
}
}
8. Verify email
function isEmail(email) {
var regx = /^([a-zA-Z0-9_\-])+@([a-zA-Z0-9_\-])+(\.[a-zA-Z0-9_\-])+$/;
return regx.test(email);
}
9. Verify ID number
The ID number may be 15 or 18 digits, 15 digits are all digits, the first 17 digits of the 18 digits are digits, and the last digit is digits or X
function isCardNo(number) {
var regx = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
return regx.test(number);
}
10. Match Chinese characters
var regx = /^[\u4e00-\u9fa5]{0,}$/;
11. Remove the leading and trailing '/'
var str = '/asdf//';
str = str.replace(/^\/*|\/*$/g, '');
12. Judge whether the date format conforms to the form of '2017-05-11', simply judge, only judge the format
var regx = /^\d{4}\-\d{1,2}\-\d{1,2}$/
13. Judge whether the date format conforms to the form of '2017-05-11', and judge strictly (complex)
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)$/;
14. Regular IPv4 address
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]?)$/;
15. Hexadecimal Color Regularization
var regx = /^#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$/;
16. Regular license plate number
var regx = /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-Z0-9]{4}[A-Z0-9挂学警港澳]{1}$/;
17. Filter HTML tags
var str="<p>dasdsa</p>nice <br> test</br>"
var regx = /<[^<>]+>/g;
str = str.replace(regx, '');
18. Regular password strength, at least 6 digits, including at least 1 uppercase letter, 1 lowercase letter, 1 number, 1 special character
var regx = /^.*(?=.{6,})(?=.*\d)(?=.*[A-Z])(?=.*[a-z])(?=.*[!@#$%^&*? ]).*$/;
19. URL regular
var regx = /^((https?|ftp|file):\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/;
20. Match floating point numbers
var regx = /^-?([1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0)$/;
21. <OPTION value="to be processed">to be processed</OPTION>
Write a regular expression that matches "<OPTION value="to be processed">"
var str = '<OPTION value="待处理">待处理</OPTION>';
var regx = /^<.*?>/;
var resiult = regx.exec(str)[0];