JavaScript高级程序设计10.pdf

String类型有几种操作字符串的方法

concat()方法拼接任意多个字符串,不修改原字符串

var stringValue=“hello ”;

var result=stringValue.concat("world");

alert(result);  //"hello world"

alert(stringValue);  //"hello"

在实际的操作中更多的使用(+)操作符

ECMAScript还提供了三个基于子字符串创建新字符串的方法:slice()、substr()和substring(),它们都接收一或两个参数,第一个参数指定子字符串的开始位置,第二个参数如不指定则直至字符串结束。

slice()和substring()第二个参数指定子字符串最后一个字符后面的位置,而substr()的第二个参数指定的是返回字符个数

var str=“hello world”;

alert(str.slice(3));  //"lo world"

alert(str.substring(3));  //"lo world"

alert(str.substr(3));  //"lo world"

alert(str.slice(3,7));  //"lo w"

alert(str.substring(3,7));  //"lo w"

alert(str.substr(3,7));  //"lo worl"

如果传入的参数是负数,三个方法的行为各不相同

var str=“hello world”;

alert(str.slice(-3));  //"rld" 将负的参数都与字符串的长度相加

alert(str.substring(-3));  //"hello world" 将负的参数都转换为0

alert(str.substr(-3));  //"rld" 将第一个负的参数与字符串的长度相加,将第二个负的参数转换为0

alert(str.slice(3,-4));  //"lo w"

alert(str.substring(3,-4));  //"hel"

alert(str.substr(3,-4));  //“”(空字符串)

字符串的位置方法

indexOf()和lastIndexOf(),前者从前往后搜索,后者往前搜索,它们都接收一个或两个参数,第一个为要搜索的字符,第二个(可选)为搜索的位置,如没有找到则返回-1

var str=“hello world”;

alert(str.indexOf("o",6));  //7

alert(str.lastIndexOf("o",6));  //4

检测它的返回值是否大于-1来判断是否存在

trim()方法会创建字符串的一个副本,删除前置以及后缀的所有空格

var str=“     hello world     ”;

var tri=str.trim();

alert(str);  //"     hello world     "

alert(tri);  //"hello world"

字符串大小写转换

var str=“hello world”;

str.toLowerCase()   str.toUpperCase()

str.toLocaleLowerCase() str.toLocaleUpperCase()  //针对少数语言(如土耳其语)Unicode大小写转换应用特殊规则

字符串的模式匹配方法

match()接收一个参数,要么是一个正则表达式要么是一个RegExp对象,match()方法返回的是一个数组

var text="cat,bat,sat,fat";

var pattern=/.at/;

//与pattern.exec(text)相同

var matches=text.match(pattern);

alert(matches.index);  //0

alert(matches[0]);  //"cat"

alert(pattern.lastIndex);  //0 语法RegExpObject.lastIndex,储存着上一次匹配文本之后第一个字符的位置

search()接收的唯一参数与match()相同,返回的是字符串中第一个匹配项的索引,如果没有找到匹配项,则返回-1,search()方法始终是从字符串开头向后查找模式

var text="cat,bat,sat,fat";

var pos=text.search(/at/);

alert(pos);  //1

replace()方法替换子字符串,接收两个参数,第一个参数可以是一个RegExp对象或者一个字符串(这个字符串不会被转换成正则表达式),第二个参数是一个字符串或者一个函数

如果第一个参数是字符串,那么会替换第一个子字符串,想要替换所有子字符串唯一办法就是提供一个正则表达式,而且要指定全局(g)标志

var text="cat,bat,sat,fat";

var result=text.replace("at","ong");

alert(result);  //"cong,bat,sat,fat"

var result=text.replace(/at/g,"ong");

alert(result);  //"cong,bong,song,fong"

另外,如果第二个参数是字符串,还可以使用一些特殊的字符序列,将正则表达式操作得到的值插入到结果字符串中

$&  匹配整个模式的子字符串

$'  匹配的子字符串之前的子字符串

$`  匹配的子字符串之后的子字符串

$n  匹配第n(0~9)个捕获组的子字符串,正则表达式里如果没有定义捕获组,则使用空字符串

$nn  匹配第nn(01~99)个捕获组的子字符串,正则表达式里如果没有定义捕获组,则使用空字符串

var text="cat,bat,sat,fat";

var result=text.replace(/(.at)/g,"word($1)");

alert(result);  //word(cat),word(bat),word(sat),word(fat)

replace()方法的第二个参数也可以是一个函数,实现更精细的替换操作

在只有一个匹配项(即与模式匹配的字符串)的情况下,会向整个函数传递3个参数:模式的匹配项、模式匹配项在字符串中的位置和原始字符串

在正则表达式中定义了多个捕获组的情况下,参数依次是模式的匹配项、第一个捕获组的匹配项、第二个捕获组的匹配项……最后两个参数仍然是模式匹配项在字符串中的位置和原始字符串

function htmlEscape(){

  return text.replace(/[<>"&]/g,function(match,pos,originalText){

    switch(match){

      case"<":

        return "&lt;";

      case">":

        return"&gt;";

      case"&":

        return"&amp;";

      case""":

        return"&quot;";

    }

  });

}

以上函数能够转义大于号、小于号、和号以及双引号

最后一个与模式匹配有关的方法是split(),可以基于指定的分隔符将字符串分割成多个子字符串,并将结果放在一个数组中,可选的第二个参数用于指定数组的大小,确保返回的数组不超过既定长度

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(/[^。]+/);  //["","。","。","。",""]

----------------------------------------------------------------------------------------------告一段落------------------------------------------------------------------------------------------------------------------

原文地址:https://www.cnblogs.com/sdgjytu/p/3705938.html