登录  /  注册
首页 > web前端 > js教程 > 正文

每天一篇javascript学习小结(String对象)_javascript技巧

php中文网
发布: 2016-05-16 15:31:29
原创
992人浏览过

1、string对象中可以传正则的函数介绍

/*
  match() 方法可在字符串内检索指定的值,或找到一个或多个正则表达式的匹配。
  该方法类似 indexOf() 和 lastIndexOf(),但是它返回指定的值,而不是字符串的位置。
  语法
  stringObject.match(searchvalue)
  stringObject.match(regexp)
  searchvalue 必需。规定要检索的字符串值。
  regexp 必需。规定要匹配的模式的 RegExp 对象。如果该参数不是 RegExp 对象,则需要首先把它传递给 RegExp 构造函数,将其转换为 RegExp 对象。
 */
 var text = "cat, bat, sat, fat"; 
 var pattern = /.at/;
 
 var matches = text.match(pattern); 
 alert(matches.index); //0
 alert(matches[0]);  //"cat"
 alert(pattern.lastIndex); //0
 /*
  定义和用法
  search() 方法用于检索字符串中指定的子字符串,或检索与正则表达式相匹配的子字符串。
  stringObject.search(regexp)
  regexp 
  该参数可以是需要在 stringObject 中检索的子串,也可以是需要检索的 RegExp 对象。
  注释:要执行忽略大小写的检索,请追加标志 i。
  返回值
  stringObject 中第一个与 regexp 相匹配的子串的起始位置。
  注释:如果没有找到任何匹配的子串,则返回 -1。
 */
 var pos = text.search(/at/);
 alert(pos); //1
 
 /*
  定义和用法
  replace() 方法用于在字符串中用一些字符替换另一些字符,或替换一个与正则表达式匹配的子串。
  stringObject.replace(regexp/substr,replacement)
  regexp/substr 
  必需。规定子字符串或要替换的模式的 RegExp 对象。
  请注意,如果该值是一个字符串,则将它作为要检索的直接量文本模式,而不是首先被转换为 RegExp 对象。
  replacement 必需。一个字符串值。规定了替换文本或生成替换文本的函数。
  返回值
  一个新的字符串,是用 replacement 替换了 regexp 的第一次匹配或所有匹配之后得到的。
 */
 var result = text.replace("at", "ond");
 alert(result); //"cond, bat, sat, fat"

 result = text.replace(/at/g, "ond");
 alert(result); //"cond, bond, sond, fond"

 result = text.replace(/(.at)/g, "word ($1)");
 alert(result); //word (cat), word (bat), word (sat), word (fat)
 
 function htmlEscape(text){
  return text.replace(/[<>"&]/g, function(match, pos, originalText){
  switch(match){
   case "<":
   return "<";
   case ">":
   return ">";
   case "&":
   return "&";
   case "\"":
   return """;
  }  
  });
 }
 
 alert(htmlEscape("<p class=\"greeting\">Hello world!</p>")); //<p class="greeting">Hello world!</p>
 /*
  定义和用法
  split() 方法用于把一个字符串分割成字符串数组。
  stringObject.split(separator,howmany)
  separator 必需。字符串或正则表达式,从该参数指定的地方分割 stringObject。
  howmany 可选。该参数可指定返回的数组的最大长度。如果设置了该参数,返回的子串不会多于这个参数指定的数组。如果没有设置该参数,整个字符串都会被分割,不考虑它的长度。
  返回值
  一个字符串数组。该数组是通过在 separator 指定的边界处将字符串 stringObject 分割成子串创建的。返回的数组中的字串不包括 separator 自身。
  但是,如果 separator 是包含子表达式的正则表达式,那么返回的数组中包括与这些子表达式匹配的字串(但不包括与整个正则表达式匹配的文本)。
 */
 var colorText = "red,blue,green,yellow";
 var colors1 = colorText.split(","); //["red", "blue", "green", "yellow"]
 var colors2 = colorText.split(",", 2); //["red", "blue"]
 var colors3 = colorText.split(/[^\,]+/); //["", ",", ",", ",", ""]
登录后复制

2、字符串转成小写和大写函数

 var stringValue = "hello world";
 alert(stringValue.toLocaleUpperCase()); //"HELLO WORLD"
 alert(stringValue.toUpperCase()); //"HELLO WORLD"
 alert(stringValue.toLocaleLowerCase()); //"hello world"
 alert(stringValue.toLowerCase()); //"hello world"
登录后复制

3、字符串对象 new String()

 var stringObject = new String("hello world");
 var stringValue = "hello world";
 
 alert(typeof stringObject); //"object"
 alert(typeof stringValue); //"string"
 alert(stringObject instanceof String); //true
 alert(stringValue instanceof String); //false
登录后复制

4、字符串fromCharCode()方法

/*
  定义和用法
  fromCharCode() 可接受一个指定的 Unicode 值,然后返回一个字符串。
  String.fromCharCode(numX,numX,...,numX)
  numX 必需。一个或多个 Unicode 值,即要创建的字符串中的字符的 Unicode 编码。
 */
 alert(String.fromCharCode(104, 101, 108, 108, 111)); //"hello"
登录后复制

5、字符串本地比较方法localeCompare()

/*
  定义和用法
  用本地特定的顺序来比较两个字符串。
  stringObject.localeCompare(target)
  target 要以本地特定的顺序与 stringObject 进行比较的字符串。
  返回值
  说明比较结果的数字。如果 stringObject 小于 target,则 localeCompare() 返回小于 0 的数。
  如果 stringObject 大于 target,则该方法返回大于 0 的数。如果两个字符串相等,或根据本地排序规则没有区别,该方法返回 0。
 */
 var stringValue = "yellow"; 
 alert(stringValue.localeCompare("brick")); //1
 alert(stringValue.localeCompare("yellow")); //0
 alert(stringValue.localeCompare("zoo")); //-1
 
 //preferred technique for using localeCompare()
 function determineOrder(value) {
  var result = stringValue.localeCompare(value);
  if (result < 0){
  alert("The string 'yellow' comes before the string '" + value + "'.");
  } else if (result > 0) {
  alert("The string 'yellow' comes after the string '" + value + "'.");
  } else {
  alert("The string 'yellow' is equal to the string '" + value + "'.");
  }
 }
 
 determineOrder("brick");
 determineOrder("yellow");
 determineOrder("zoo");
登录后复制

6、indexOf() 和 lastIndexOf()方法

/*
  定义和用法
  indexOf() 方法可返回某个指定的字符串值在字符串中首次出现的位置。
  stringObject.indexOf(searchvalue,fromindex)
  searchvalue 必需。规定需检索的字符串值。
  fromindex 可选的整数参数。规定在字符串中开始检索的位置。
  它的合法取值是 0 到 stringObject.length - 1。如省略该参数,则将从字符串的首字符开始检索。
  
  定义和用法
  lastIndexOf() 方法可返回一个指定的字符串值最后出现的位置,在一个字符串中的指定位置从后向前搜索。
  stringObject.lastIndexOf(searchvalue,fromindex)
  searchvalue 必需。规定需检索的字符串值。
  fromindex 可选的整数参数。规定在字符串中开始检索的位置。
  它的合法取值是 0 到 stringObject.length - 1。如省略该参数,则将从字符串的最后一个字符处开始检索。
 */
 var stringValue = "hello world";
 alert(stringValue.indexOf("o"));  //4
 alert(stringValue.lastIndexOf("o")); //7
 alert(stringValue.indexOf("o", 6));  //7
 alert(stringValue.lastIndexOf("o", 6)); //4 
登录后复制

7、字符串常用字符截取方法

/*
  定义和用法
  slice() 方法可从已有的数组中返回选定的元素。
  arrayObject.slice(start,end)
  start 必需。规定从何处开始选取。如果是负数,那么它规定从数组尾部开始算起的位置。也就是说,-1 指最后一个元素,-2 指倒数第二个元素,以此类推。
  end 可选。规定从何处结束选取。该参数是数组片断结束处的数组下标。如果没有指定该参数,那么切分的数组包含从 start 到数组结束的所有元素。
  如果这个参数是负数,那么它规定的是从数组尾部开始算起的元素。
  返回值
  返回一个新的数组,包含从 start 到 end (不包括该元素)的 arrayObject 中的元素。
  说明
  请注意,该方法并不会修改数组,而是返回一个子数组。如果想删除数组中的一段元素,应该使用方法 Array.splice()。
  
  
  定义和用法
  substring() 方法用于提取字符串中介于两个指定下标之间的字符。
  语法
  stringObject.substring(start,stop)
  start 必需。一个非负的整数,规定要提取的子串的第一个字符在 stringObject 中的位置。
  stop 可选。一个非负的整数,比要提取的子串的最后一个字符在 stringObject 中的位置多 1。如果省略该参数,那么返回的子串会一直到字符串的结尾。
  返回值
  一个新的字符串,该字符串值包含 stringObject 的一个子字符串,其内容是从 start 处到 stop-1 处的所有字符,其长度为 stop 减 start。
  说明
  substring() 方法返回的子串包括 start 处的字符,但不包括 stop 处的字符。
  如果参数 start 与 stop 相等,那么该方法返回的就是一个空串(即长度为 0 的字符串)。
  如果 start 比 stop 大,那么该方法在提取子串之前会先交换这两个参数。
  
  
  定义和用法
  substr() 方法可在字符串中抽取从 start 下标开始的指定数目的字符。
  语法
  stringObject.substr(start,length)
  start 必需。要抽取的子串的起始下标。必须是数值。如果是负数,那么该参数声明从字符串的尾部开始算起的位置。
   也就是说,-1 指字符串中最后一个字符,-2 指倒数第二个字符,以此类推。
  length 可选。子串中的字符数。必须是数值。如果省略了该参数,那么返回从 stringObject 的开始位置到结尾的字串。
  返回值
  一个新的字符串,包含从 stringObject 的 start(包括 start 所指的字符) 处开始的 length 个字符。
  如果没有指定 length,那么返回的字符串包含从 start 到 stringObject 的结尾的字符。
  
  
 */
 var stringValue = "hello world";
 alert(stringValue.slice(3)); //"lo world"
 alert(stringValue.substring(3)); //"lo world"
 alert(stringValue.substr(3)); //"lo world"
 alert(stringValue.slice(3, 7)); //"lo w"
 alert(stringValue.substring(3,7)); //"lo w"
 alert(stringValue.substr(3, 7)); //"lo worl"
 
 alert(stringValue.slice(-3));  //"rld"
 alert(stringValue.substring(-3)); //"hello world"
 alert(stringValue.substr(-3)); //"rld"
 alert(stringValue.slice(3, -4)); //"lo w"
 alert(stringValue.substring(3, -4)); //"hel"
 alert(stringValue.substr(3, -4)); //"" (empty string)
登录后复制

以上就是今天的javascript学习小结,之后每天还会继续更新,希望大家继续关注。

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号