zoukankan      html  css  js  c++  java
  • js-string对象

    ** 创建string对象
      *** var str ="acb";
      ** 方法和属性
        ** 属性 length:字符串的长度

        ** 方法
        (1)与HTML相关的方法
          - bold():加粗
          - fontcolor():设置字体的颜色
          - fontsize():设置字体的大小(1-7)
          - link():将字符串显示成超链接
          - sub() sup():下标和上标

        (2)与Java相似的方法

          - concat():连接字符串

    var str1 = "abc";
    var str2 = "def";
    document.write(str1.concat(str2));

          - charAt():返回指定位置的字符串

    var str3 = "lizike";
    document.write(str3.charAt(2));    //字符位置不存在,返回空字符串

          - indexOf():返回字符串位置

    var str4 = "world";
    document.write(str4.indexOf("l"));
    document.write("<br/>");
    document.write(str4.indexOf("W"));    //字符不存在,返回-1

          - split():切分字符串,成数组

    var str5 = "a-b-c-d";
    var arr1 = str5.split("-");
    document.write(arr1);
    document.write("<br/>");
    document.write("length:" + arr1.length);

          - replace():替换字符串

    var str6 = "abcde";
    document.write(str6.replace("a","Q"));

          - substr() 和 substring()

    // substr 从第几位开始,向后截取几位
    var str7 = "wangyijun love lily";
    document.write(str7.substr(10, 4));    //love 从第10位开始,向后截取4个字符
    // substring 从第几位开始,到第几位结束,但是不包含最后一位
    document.write(str7.substring(10,14));    // love 从第几位开始到第几位结束 [10,14)

        - test()  常用正则表达式

    1.正整数            /^[0-9]*[1-9][0-9]*$/
    
    2.整数               /^-?d+$/
    
    3.数字              /^d+(.{1}d+)?$/
    
    4.由26个英文字母组成的字符串                     /^[A-Za-z]+$/
    
    5.email地址             /^[w-]+(.[w-]+)*@[w-]+(.[w-]+)+$/
    
         或者                      /w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*/
    
    6.url地址                 /^[a-zA-z]+://(w+(-w+)*)(.(w+(-w+)*))*(?S*)?$/
    
         或者                      /http://([w-]+.)+[w-]+(/[w- ./?%&=]*)?/
    
    7.匹配中文字符的正则             /[u4e00-u9fa5]/
    
    8.匹配空行的正则              /n[s| ]*r/
    
    9.匹配HTML标记的正则             /<(.*)>.*</1>|<(.*) />/
    
    10.匹配首尾空格的正则               /(^s*)|(s*$)/
    
             应用:javascript中没有像vbscript那样的trim函数,我们就可以利用这个表达式来实现,如下:
                 String.prototype.trim = function(){
                     return this.replace(/(^s*)|(s*$)/g, "");
                 }
    
    11.匹配IP地址的正则              /(d+).(d+).(d+).(d+)/
    
    12.限制表单文本框输入内容
    
            只能输入中文:
                 onkeyup="value=value.replace(/[^u4E00-u9FA5]/g,'')" 
                     onbeforepaste="clipboardData.setData('text',
                     clipboardData.getData('text').replace(/[^u4E00-u9FA5]/g,''))"
    
            只能输入数字:
                 onkeyup="value=value.replace(/[^d]/g,'')" 
                     onbeforepaste="clipboardData.setData('text',
                     clipboardData.getData('text').replace(/[^d]/g,''))"
    
            只能输入数字和英文:
                 onkeyup="value=value.replace(/[W]/g,'')" 
                     onbeforepaste="clipboardData.setData('text',
                     clipboardData.getData('text').replace(/[^d]/g,''))"
  • 相关阅读:
    使用C# lock同时访问共享数据
    将两个DataTable合并成一个DataTable
    嵌套存储过程返回值的调用
    在 Sql Server 中使用 MD5 加密
    用DIV制作即时提示层 防止被select控件遮挡的方法
    操作Cookie公用代码
    JS实现回调例子
    ASP存储过程参数数据类型
    在asp中使用js的encodeURIComponent方法
    Uva 10250 The Other Two Trees
  • 原文地址:https://www.cnblogs.com/ibabyli/p/9894264.html
Copyright © 2011-2022 走看看