xiaowei-blog

导航

javascript技巧篇(2)

一.利用split()截取字符串

  说明:split()函数返回的是一个字符串数组,在字符串中出现的需要截取的字符的位置都要进行分解。所以在调用截取结果的时候需要获取返回数组中的值

 例:根据邮箱地址获取该用户的邮箱用户名称

    var  mail=document.getElementById("mail");.value.toString();//获取用户邮箱地址

    var username=mail.split("@");//获取字符'@'之前的用户名

   document.getElementById("mail");.value=username[0];//获取控件值

二.使用正则表达式去掉字符串中的空格

   /*去除首尾空格的方法 */

     function trim(str)

     {

       var  re=/^\s*/g;

       str=str.replace(re,"");

     return str; 

    }

   /*去除字符串中全部空格的方法*/

    function allTrim(str){

       var re=/\s/g;

      str=str.replace(re,"");

       return str;

     }

 

posted on 2014-10-21 09:34  xiaowei-blog  阅读(110)  评论(0编辑  收藏  举报

欢 迎 大 家光 临 我 的 个 人 博 客 !