Regular expressions, there is no one like me, do not know how many times to learn, learning time seems to understand, after a period of time and then forget almost, and so on really need to use, or a face mengbi. In the final analysis, it is not enough practice, has been in the extent of watching but not doing. So I collected these regular exercises to make sure I learned something new. It is suggested that readers can do it themselves after reading the topic, and then look at the implementation method. This article does not cover the basics, only record exercises, if there are new questions, will keep updated.

Var s1 = “get-element-by-id”; Given such a concatenated string, write a string getElementById that function converts to a hump nomenclature

var f = function(s) {
    return s.replace(/-\w/g, function(x) {
        returnx.slice(1).toUpperCase(); })}Copy the code

2. Check whether the string contains numbers

function containsNumber(str) {
    var regx = /\d/;
    return regx.text(str);
}
Copy the code

3. Judge the phone number

function isPhone(tel) {
    var regx = /^1[34578]\d{9}$/;
    return regx.test(tel);
}
Copy the code

4. Judge whether it conforms to the specified format

Given the string STR, check whether it matches the following format

  1. XXX-XXX-XXXX
  2. X is the Number type
function matchesPattern(str) {
    return /^(\d{3}-){2}\d{4}&/.test(str);
}
Copy the code

5. Determine whether it conforms to USD format

Given a string STR, check if it conforms to the dollar format

  1. Start with $
  2. Part of the integer, starting with the ones place, the full three digits are separated by theta
  3. If it is a decimal, the decimal part has length 2
  4. The correct format is $1,023,032.03 or $2.03, and the wrong format is $3,432,12.12 or $34,344.3**
functionIsUSD (STR) {var regx = / ^ $\ \ d {1, 3} (\ d {3}) * (. \ \ d {2})? $/;return regx.test(str);
}
Copy the code

JS implementation of thousands separator

functionFormat (number) {var regx = /\d{1,3}(? =(\d{3})+$)/g;return (number + ' ').replace(regx, '$&,) // $& represents the string matching regx}Copy the code

7. Get URL parameters

Gets the parameters in the URL

  1. Specifies the parameter name, and returns either the value of the parameter or an empty string
  2. Return all argument objects or {} without specifying argument names
  3. If multiple arguments with the same name exist, an array is returned
functiongetUrlParam(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) {
                returnarr[ele]; }}return ' '; }}Copy the code

8. Verify email

function isEmail(email) {
    var regx = /^([a-zA-Z0-9_\-])+@([a-zA-Z0-9_\-])+(\.[a-zA-Z0-9_\-])+$/;
    return regx.test(email);
}
Copy the code

9. Verify your ID number

The ID number may be 15 or 18 digits, with 15 digits being all digits, the first 17 digits being digits, and the last digit being either a digit or an X

function isCardNo(number) {
    var regx = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
    return regx.test(number);
}
Copy the code

10. Match Chinese characters

var regx = /^[\u4e00-\u9fa5]{0,}$/;
Copy the code

11. Remove the ‘/’ from both ends

var str = '/asdf//';
str = str.replace(/^\/*|\/*$/g, ' ');
Copy the code

12, judge whether the date format conforms to the form of ‘2017-05-11’, simple judgment, only judge the format

Var regx = / ^ \ d {4} \ \ d {1, 2} \ - \ d {1, 2} $/Copy the code

13, judge whether the date format conforms to the form of ‘2017-05-11’, strictly judge (more complex)

var regx = /^(? : (? ! 0000) [0-9] {4} - (? : (? : 0 | [1-9] [0-2] 1) - (? : 0 [1-9] [0-9] | | 1 2 [0 to 8]) | (? : 0 [9] 13 - | [0-2] 1) - (? 30) : 29 | | (? : 0 [13578] 1 [02]) - 31) | | (? : [0-9] {2} (? : 0 [48] | [2468] [048] | [13579] [26]) | (? : 0 [48] | [2468] [048] | [13579] [26]) 00) - 02-29) $/;Copy the code

14. The IPv4 address is regular

var regx = /^(? : (? : 25 [0 to 5] | 2 [0 to 4] [0-9] | [01]? [0-9] [0-9]?) \.) {3} (? : 25 [0 to 5] | 2 [0 to 4] [0-9] | [01]? [0-9] [0-9]?) $/;Copy the code

15, hexadecimal color regular

var regx = /^#? ([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$/;
Copy the code

16. The license plate is regular

Var regx = / ^ [beijing-tianjin Shanghai yu ji yu cloud liao black xiang anhui new GuiGan of hubei province can Sue your yue jin meng shan ji min qinghai-tibet plain NingQiong that brought A - Z] {1} {1} [a-z] [A - Z0-9] {4} [A - Z0-9 hang cop Hong Kong and Macao] {1} $/;Copy the code

Filter HTML tags

var str="<p>dasdsa</p>nice <br> test</br>"
var regx = /<[^<>]+>/g;
str = str.replace(regx, ' ');
Copy the code

18. The password is regular and contains at least six characters, including at least one uppercase letter, one lowercase letter, one digit, and one special character

var regx = /^.*(? (=. {6})? =.*\d)(? =.*[A-Z])(? =.*[a-z])(? =. * [! @# $% ^ & *? ] ). * $/;
Copy the code

19. URL regularization

var regx = /^((https? |ftp|file):\/\/)? ([\ \. Da - z -] +) \. ([a-z \.] {2, 6}) (/ \ \ / \ w - *) * \ /? $/;Copy the code

20. Match floating point numbers

var regx = /^-? ([1-9]\d*\.\d*|0\.\d*[1-9]\d*|0? \. | 0 + 0) $/;Copy the code

21, <OPTION value=” OPTION “>

Write a regular expression that matches “<OPTION value=” pending “>”

var str = ''; var regx = /^<.*? > /; var resiult = regx.exec(str)[0];Copy the code

Finally, regulex is a great regex practice site to see where regex matches are going

If you like, please follow me on Github and give me a Star, I will share some JS knowledge regularly, ^_^