日期:2014-05-16  浏览次数:20554 次

使用JavaScript正则表达式实现一些实用函数
//去除字符串空格
/*** Start of Modify ***/
function ltrim(str){
    /* Trims leading spaces - returns string
     * Returns trimmed string
     */
    return str.replace(/^\s+/, '');
}//eof - ltrim

function rtrim(str) {
    /* Trims trailing spaces - returns string
     * Returns trimmed string
     */
    return str.replace(/\s+$/, '');
}//eof - rtrim

function alltrim(str) {
    /* Trims leading and trailing spaces
     * Returns trimmed string
     */
    return str.replace(/^\s+|\s+$/g, '');
}//eof - alltrim

function padleft(str, ch, num) {
    /* Pads string on left with number characters
     * Args:
     *    str = string to pad
     *    ch = pad character
     *    num = number of total characters in final string
     * returns string
     */
	ch = ch || " "; //otherwise pad.length remains 0.
    
	//Create pad string num chars
    var pad = "";
    do  {
        pad += ch;
    } while(pad.length < num);

    //Regular express take num chars from right
    var re = new RegExp(".{" + num + "}$")[0];

    return re.exec(pad + str);
}//eof - padleft

function padright(str, ch, num){
    /* Pads string on right with number characters
     * Args:
     *    str = string to pad
     *    ch = pad character
     *    num = number of total characters in final string
     * returns string
     */
	ch = ch || " "; //otherwise pad.length remains 0.

    //Create pad string num chars
    var pad = "";
	do {
        pad += ch;
    } while (pad.length < num);

    //Regular express take num chars from left
    var re = new RegExp("^.{" + num + "}");
    return re.exec(str + pad)[0];
}//eof - padright

function padcenter(str, ch, size, extra2right) {
    /* Centers string in a string by padding on right and left
     * Args:
     *    str = string to center
     *    ch = pad character
     *    size = number of total characters in final string
	 *    extra2right = optional true if extra character should be on right
     * returns string
     */
    var padStr = "";
	var re;
	var len = str.length;
	var rtrnVal;

	if (len <= size) {
		if (extra2right)
			re = new RegExp("^(.*)(.{" + len + "})(\\1)");
		else
			re = new RegExp("(.*)(.{" + len + "})(\\1)$");

		do {
			padStr += ch;
		} while (--size);
		
		rtrnVal = padStr.replace(re, "$1" + str +"$3");
	} else {
		rtrnVal = extractMiddle(str, size, extra2right);
	}
	return rtrnVal;
}//eof padcenter

function centerInStr(inStr, outStr, extra2right) {
    /* Centers string in a string of same characters
     * Args:
     *    inStr = string to center
     *    outStr = string of same character 
	 *    extra2right = optional true if extra character should be on right
     * returns string
     */
    var padStr = "";
	var re;
	var len = inStr.length;
	var size = outStr.length;
	var rtrnVal;

	if (len <= size) {
		if (extra2right)
			re = new RegExp("^(.*)(.{" + len + "})(\\1)");
		else
			re = new RegExp("(.*)(.{" + len + "})(\\1)$");

		rtrnVal = outStr.replace(re, "$1" + inStr +"$3");
	} else {
		rtrnVal = extractMiddle(inStr, size, extra2right);
	}
	return rtrnVal;
}//eof centerInStr

function centerInStr2(inStr, outStr, extra2right) {
    /* Centers string in a string of mixed characters replacing characters
     * Args:
     *    inStr = string to center within outStr
	 *    outStr = the outer string
	 *    extra2right = optional true if extra character should be on right
     * returns string
     */
	var inSize = inStr.length;
	var outSize = outStr.length;
    var l = Math.floor( (outSize - inSize) /2);
    var re;
	var rtrnVal;

	if (inSize <= outSize) {
		if (extra2right)
			re = new RegExp("(.{"+l+"})(.{" + inSize + "})(.*)");
		else
			re = new RegExp("(.*)(.{" + inSize + "})(.{"+l+"})");

		rtrnVal = outStr.replace(re, "$1" + inStr + "$3");
	} else {
		rtrnVal = extractMiddle(inStr, outSize, extra2right);
	}

	return rtrnVal;
}//eof centerInStr2

function centerInStr3(inStr, outStr, extra2right) {