Exercises with regular expressions

Today, I did a few programming topics and felt the power of regular expressions.

First, check whether the string contains numbers. Returns true if it is included, false otherwise.

 	function containsNumber(str) {                 
               for (var i = 0; i < str.length; i++) {
                     if (!isNaN(parseInt(str[i]))) { //Determine whether it is a number
                         return true;                       
                     }
               }
               return false;
        }

parseInt(string, radix) : Parses a string as a number. radix is ​​the base. The default is a decimal number.

                                     NaN is returned if the first character of the string cannot be converted to a number.

isNaN(): Checks if a value is a non-numeric value. Returns true if the parameter value is NaN or a non-numeric value such as a string, object, undefined, etc., otherwise returns false.

Use regular expressions as:

function containsNumber(str) { 			   
 	var reg = / \ d /;
        return reg.test(str); 			   
}

2. Given a string str, check whether it contains consecutive repeated letters (a-zA-Z), return true if it contains, otherwise return false

function containsRepeatingLetter(str) {

        return /([a-zA-Z])\1/.test(str);
    }

(): represents a capturing group,

\1 represents a reference to the first capturing group

3. Get the parameters in the url  
    1. Specify the parameter name, return the value of the parameter or an empty string  
    2. Do not specify the parameter name, return all parameter objects or {} 

    3. If there are multiple parameters with the same name, return an array**

function getUrlParam(sUrl, sKey) {

	      var arr = {};
		sUrl.replace(/\??(\w+)=(\w+)$?/g,function (match,p1,p2) {
			if (!arr[p1]) {
				arr[p1]=p2;
			}else{
				p=arr[p1];
				arr[p1]=[].cancat(p,p2);
			}
		});

		if(!sKey){
			return arr;
		}else{
			for(var ele in arr){
				if (ele=sKey) {
					return arr[it];
				}
			}
			return "";
		}	
	}

   Note: 1. The second parameter of the repalce function can be the replacement text or a function that generates the replacement text. The parameters of the function, match is the sub-capturing group, and p1, p2,...pn are the items of the capturing group.

             2. g stands for global attributes. This means that after finding a matching value, continue backtracking.

             3. If the second parameter of replace is a function, the parameters of the function are as follows

               

Fourth, the conversion of camel case naming

function campCase (str) {
		var reg=/-([a-z])/g;
		return str.replace(reg,function (match,p1) {
		      return p1.toUpperCase();
		});
}

Note: The return value of 1.repalce is the replaced string.

           2..match is the matching substring. (include-)

           3.p1 is the first capturing group.


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325888114&siteId=291194637