回覆列表
  • 1 # Amo晴天657

    <script>

    function subString(str, len, hasDot)

    {

    var newLength = 0;

    var newStr = "";

    var chineseRegex = /[^\x00-\xff]/g;

    var singleChar = "";

    var strLength = str.replace(chineseRegex,"**").length;

    for(var i = 0;i < strLength;i++){

    singleChar = str.charAt(i).toString();

    if(singleChar.match(chineseRegex) != null){

    newLength += 2;

    }else{

    newLength++;

    }

    if(newLength > len){

    break;

    }

    newStr += singleChar;

    }

    if(hasDot && strLength > len){

    newStr += "...";

    }

    return newStr;

    }

    alert(subString("js擷取中英文字串無亂碼",10,true));

    </script>

  • 中秋節和大豐收的關聯?
  • 全明星要來了,賽季至今,哪些球隊的表現最出乎你的意料?