转行学前端的第 27 天 : 了解 ECMAScript String 数据类型 (中篇)

519 阅读21分钟

我是小又又,住在武汉,做了两年新媒体,准备用 6 个月时间转行前端。

今日学习目标


昨天基于一些页面搜索,学习了《JavaScirpt 高级程序设计》(第三版)  第 3 章节中的 ES 基础概念中基础数据类型,昨天其实有看到 String 有很多方法和属性,所以,今天主要是基于搜索来仔细学习 String 数据类型,又是适合学习的一天,加油,小又又!!!!


今日学习概要


主要是从下面几个方面详细学习 字符串 数据类型

  • 字符串定义
  • 字符串的属性
  • 字符串的柔和方法
  • 字符串的强硬方法
  • 对象通用方法
  • 字符串字面量的原型方法
  • 字符串方法总结


今天,小又又尝试了一个不太同的描述方式,花了很多时间,查了资料,将多个页面进行整理归纳,期望大家能够接受~~~~

这边推荐一个网站,也是今天在 MDN JS Shells 中看到的推荐 jsconsole~~~~~

同样由于字数问题,今天文章分了三篇上传~~~~

  • 上篇是包括(字符串定义,字符串的属性,字符串的柔和方法部分方法)
  • 中篇是包括(字符串的柔和方法部分方法)
  • 下篇是包括(字符串的柔和方法部分方法,字符串的强硬方法和对象通用方法,字符串字面量的原型方法,字符串方法总结和今日总结)

检索

概述

方法名 描述 参数
indexOf(searchValue[, fromIndex]) 从字符串对象中返回首个被发现的给定值的索引值开始在 fromIndex进行搜索。如果未找到该值,则返回-1。 @param searchValue 一个字符串表示被查找的值。@param fromIndex 可选 表示调用该方法的字符串中开始查找的位置。可以是任意整数。默认值为 0。如果 fromIndex < 0 则查找整个字符串(如同传进了 0)。如果 fromIndex >= varName.length,则该方法返回 -1,除非被查找的字符串是一个空字符串,此时返回 varName.length。
lastIndexOf(searchValue[, fromIndex]) 返回指定值在调用该方法的字符串中最后出现的位置,如果没找到则返回 -1。从该字符串的后面向前查找,从 fromIndex 处开始。 @param searchValue 一个字符串,表示被查找的值。@param fromIndex 从调用该方法字符串的此位置处开始查找。可以是任意整数。默认值为 str.length。如果为负值,则被看作 0。如果 fromIndex > str.length,则 fromIndex 被看作 str.length。
includes(searchString[, position]) 判断一个字符串是否包含在另一个字符串中,根据情况返回true或false。 @param searchString 要在此字符串中搜索的字符串。@param position 可选。从当前字符串的哪个索引位置开始搜寻子字符串;默认值为0。
startsWith(searchString [, position]) 判断字符串的起始位置是否匹配其他字符串中的字符。 @param searchString 要搜索的子字符串。@param position 在 varName 中搜索 searchString 的开始位置,默认值为 0,也就是真正的字符串开头处。
endsWith(searchString [, position]) 判断一个字符串的结尾是否包含其他字符串中的字符。 @param searchString 要搜索的子字符串。@param position 在 varName 中搜索 searchString 的结束位置,默认值为 varName.length,也就是真正的字符串结尾处。
search(regexp) 对正则表达式和指定字符串进行匹配搜索,返回第一个出现的匹配项的下标。如果匹配成功,则 search() 返回正则表达式在字符串中首次匹配项的索引。否则,返回 -1。 @param regexp 一个正则表达式(regular expression)对象。如果传入一个非正则表达式对象,则会使用 new RegExp(obj) 隐式地将其转换为正则表达式对象。
match(regexp) 将字符串与正则表达式匹配,并返回一个包含该搜索结果的数组。返回值array,一个包含了整个匹配结果以及任何括号捕获的匹配结果的 Array ;如果没有匹配项,则返回 null 。 @param regexp 一个正则表达式对象。如果传入一个非正则表达式对象,则会隐式地使用 new RegExp(obj) 将其转换为一个 RegExp 。如果你未提供任何参数,直接使用 match() ,那么你会得到一个包含空字符串的 Array :[""] 。

详细方法

1) indexOf(searchValue[, fromIndex])

  • 指定值的第一次出现的索引; 如果没有找到 -1。
  • 对于字符串中原本包含空格,tab缩进,tab键和空格键产生的空白占位格是不同的,indexOf方法会进行严格的匹配。
  • 对于字符串中原本包含字母,indexOf方法会进行严格的大小写匹配。
  • 对于字符串中包含的BMP字符,会严格匹配编码格式。
使用方法 结果
oString.indexOf(" ") 5
oString.indexOf("hell") 0
oString.indexOf("helLo") -1
oString.indexOf("hell",3) -1
oString_1.indexOf("\uD87E\uDC04") 2
oString_1.indexOf("你") -1
oString_1.normalize("NFC").indexOf("你") 1
tString_2.indexOf("  ")//空格键产生的空白占位格 -1
tString_2.indexOf(" ")//tab键产生的空白占位格 13
oString_1.indexOf("sss") -1

第一个参数错误示例

  • 进行匹配的时候,只要原始值是定义好的,加没加双引号都可以被正常检测出来。
  • 布尔值字符串可以判断是否包含布尔值对象。
  • 自己判断是否完全包含自己是可以正常检测的。
  • 传入空字符串的时候,indexOf方法会直接返回0
使用方法 结果
"true".indexOf(true) 0
"true".indexOf("true") 0
"true".indexOf(oBool) 0
"false".indexOf(false) 0
"false".indexOf("false") 0
"false".indexOf(oBool) -1
"null".indexOf(null) 0
"null".indexOf("null") 0
"undefined".indexOf(undefined) 0
"undefined".indexOf("undefined") 0
"NaN".indexOf(NaN) 0
"NaN".indexOf("NaN") 0
oString.indexOf("") 0
oString.indexOf(oString) 0
oString.indexOf(true) -1
oString.indexOf(false) -1
oString.indexOf(null) -1
oString.indexOf(undefined) -1
oString.indexOf(NaN) -1

第二个参数错误示例

  • 索引值的范围是0~varName.length-1
  • 传入的数字是向下取整的,只取数字的整数部分,举个栗子,比如传入0.1-0.9,indexOf方法是当作0进行处理。
  • 对于传入的负值整数,indexOf方法是当作0进行处理。
  • 对于传入的StringObjectArray类型的值,indexOf方法是当作0进行处理。
  • 对于Boolean类型的值,indexOf方法会将true转化成1false转化成0,进行处理。
  • 对于传入的Date类型的值,indexOf方法会自动转成对应的时间戳数值,进行检索匹配。
  • 对于Number.NaNNumber.MIN_VALUENumber.NEGATIVE_INFINITYindexOf方法是当作0进行处理。
  • 对于Number.MAX_VALUENumber.POSITIVE_INFINITYindexOf方法是当作超出varName.length进行处理。
使用方法 结果
oString.indexOf("hell",0.1) 0
oString.indexOf("hell",0.5) 0
oString.indexOf("hell",0.8) 0
oString.indexOf("hell",1) 0
oString.indexOf("hell",1.1) 0
oString.indexOf("hell",1.5) 0
oString.indexOf("hell",1.8) 0
oString.indexOf("hell",oString.length) -1
oString.indexOf("hell",-2) 0
oString.indexOf("hell",-3) 0
oString.indexOf("hell",true) -1
oString.indexOf("hell",false) 0
oString.indexOf("hell","true") 0
oString.indexOf("hell","false") 0
oString.indexOf("hell",oo) 0
oString.indexOf("hell",oBool) -1
oString.indexOf("hell",oArray) 0
oString.indexOf("hell",oDate) -1
oString.indexOf("hell",Number.NaN) 0
oString.indexOf("hell",Number.MAX_VALUE) -1
oString.indexOf("hell",Number.MIN_VALUE) 0
oString.indexOf("hell",Number.NEGATIVE_INFINITY) 0
oString.indexOf("hell",Number.POSITIVE_INFINITY) -1

2) lastIndexOf(searchValue[, fromIndex])

  • 对于字符串中原本包含空格,tab缩进,tab键和空格键产生的空白占位格是不同的,lastIndexOf方法会进行严格的匹配。
  • 对于字符串中原本包含字母,lastIndexOf方法会进行严格的大小写匹配。
  • 对于字符串中包含的BMP字符,会严格匹配编码格式。
  • indexOf方法相比,lastIndexOf方法有更好的容错性,会对第二个参数做更多的处理。
使用方法 结果
oString.lastIndexOf(" ") 5
oString.lastIndexOf("hell") 0
oString.lastIndexOf("helLo") -1
oString.lastIndexOf("hell",3) -1
oString_1.lastIndexOf("\uD87E\uDC04") 2
oString_1.lastIndexOf("你") -1
oString_1.normalize("NFC").lastIndexOf("你") 1
tString_2.lastIndexOf("  ")//空格键产生的空白占位格 -1
tString_2.lastIndexOf(" ")//tab键产生的空白占位格 13
oString_1.lastIndexOf("sss") -1

第一个参数错误示例

  • 进行匹配的时候,只要原始值是定义好的,加没加双引号都可以被正常检测出来。
  • 布尔值字符串可以判断是否包含布尔值对象。
  • 自己判断是否完全包含自己是可以正常检测的。
  • 传入空字符串的时候,lastIndexOf方法会直接返回varName.length
使用方法 结果
"true".lastIndexOf(true) 0
"true".lastIndexOf("true") 0
"true".lastIndexOf(oBool) 0
"false".lastIndexOf(false) 0
"false".lastIndexOf("false") 0
"false".lastIndexOf(oBool) -1
"null".lastIndexOf(null) 0
"null".lastIndexOf("null") 0
"undefined".lastIndexOf(undefined) 0
"undefined".lastIndexOf("undefined") 0
"NaN".lastIndexOf(NaN) 0
"NaN".lastIndexOf("NaN") 0
oString.lastIndexOf("") 11
oString.lastIndexOf(oString) 0
oString.lastIndexOf(true) -1
oString.lastIndexOf(false) -1
oString.lastIndexOf(null) -1
oString.lastIndexOf(undefined) -1
oString.lastIndexOf(NaN) -1

第二个参数错误示例

  • 索引值的范围是1~varName.length
  • 传入的大于0的数字是向下取整的,只取数字的整数部分,举个栗子,比如传入1.1-1.9,lastIndexOf方法是当作1进行处理。
  • 对于传入的0和负值整数,lastIndexOf方法是当作1进行处理。
  • 对于传入的StringObjectArray类型的值,lastIndexOf方法是当作1进行处理。
  • 对于Boolean类型的值,indexOf方法会将true转化成1false转化成0,进行处理。
  • 对于传入的Date类型的值,lastIndexOf方法会自动转成对应的时间戳数值,进行检索匹配。
  • 对于Number.NaNNumber.MIN_VALUENumber.NEGATIVE_INFINITYindexOf方法是当作1进行处理。
  • 对于Number.MAX_VALUENumber.POSITIVE_INFINITYindexOf方法是当作varName.length进行处理。
使用方法 结果
oString.lastIndexOf("hell",0.1) 0
oString.lastIndexOf("hell",0.5) 0
oString.lastIndexOf("hell",0.8) 0
oString.lastIndexOf("hell",1) 0
oString.lastIndexOf("hell",1.1) 0
oString.lastIndexOf("hell",1.5) 0
oString.lastIndexOf("hell",1.8) 0
oString.lastIndexOf("hell",oString.length) 0
oString.lastIndexOf("hell",-2) 0
oString.lastIndexOf("hell",-3) 0
oString.lastIndexOf("hell",true) 0
oString.lastIndexOf("hell",false) 0
oString.lastIndexOf("hell","true") 0
oString.lastIndexOf("hell","false") 0
oString.lastIndexOf("hell",oo) 0
oString.lastIndexOf("hell",oBool) 0
oString.lastIndexOf("hell",oArray) 0
oString.lastIndexOf("hell",oDate) 0
oString.lastIndexOf("hell",Number.NaN) 0
oString.lastIndexOf("hell",Number.MAX_VALUE) 0
oString.lastIndexOf("hell",Number.MIN_VALUE) 0
oString.lastIndexOf("hell",Number.NEGATIVE_INFINITY) 0
oString.lastIndexOf("hell",Number.POSITIVE_INFINITY) 0

3) includes(searchString[, position])


这个方法已经被加入到 ECMAScript 6标准中,但未必在所有的JavaScript实现中都可以使用。

在进行内容匹配的时候,这个方法是区分大小写的。

Firefox 18 - 39中,这个方法的名称叫contains()。由于下面的理由,在bug 1102219中,它被重命名为includes()

据报道,在Firefox 17上,一些使用MooTools 1.2的网站会崩溃掉。这个版本的MooTools会检查函数String.prototype.contains()是否存在,如果不存在的话,MooTools就添加它自己的函数。 通过在Firefox 17中引入这个函数,检查更改的行为在一定程度上导致了基于MooToolsString.prototype.contains()函数的代码实现中断。结果是,当MooTools的拓展导致MooTools 1.2.6版本的发布,此实现在Firefox 17中不可用和String.prototype.contains()在随后一个版本Firefox 18上是可用的。

MooTools 1.3会强制使用它自己版本的函数String.prototype.contains(),因此,依赖它的网站不会崩溃掉。然而,你应该注意此方法在MooTools 1.3 签名和ECMAScript 6 签名中的不同(在第二个参数)。后来,为了与ES6标准一致在MooTools 1.5版本及以上更改了签名。

  • 对于字符串中原本包含空格,tab缩进,tab键和空格键产生的空白占位格是不同的,includes方法会进行严格的匹配。
  • 对于字符串中原本包含字母,includes方法会进行严格的大小写匹配。
  • 对于字符串中包含的BMP字符,会严格匹配编码格式。
使用方法 结果
oString.includes(" ") true
oString.includes("llo") true
oString.includes("lLo") false
oString.includes("llo",3) false
oString_1.includes("\uD87E\uDC04") true
oString_1.includes("你") false
oString_1.normalize("NFC").includes("你") true
tString_2.includes("  ")//空格键产生的空白占位格 false
tString_2.includes(" ")//tab键产生的空白占位格 true
oString_1.includes("sss") false

第一个参数错误示例

  • 进行匹配的时候,只要原始值是定义好的,加没加双引号都可以被正常检测出来。
  • 布尔值字符串可以判断是否包含布尔值对象。
  • 自己判断是否完全包含自己是可以正常检测的。
  • contains()已经废弃。
  • 传入空字符串的时候,includes方法会直接返回true
使用方法 结果
"true".includes(true) true
"true".includes("true") true
"true".includes(oBool) true
"false".includes(false) true
"false".includes("false") true
"false".includes(oBool) false
"null".includes(null) true
"null".includes("null") true
"undefined".includes(undefined) true
"undefined".includes("undefined") true
"NaN".includes(NaN) true
"NaN".includes("NaN") true
oString.includes("") true
oString.includes(oString) true
varName.contains() 直接报错

第二个参数错误示例

  • 索引值的范围是0~varName.length-1
  • 传入的数字是向下取整的,只取数字的整数部分,举个栗子,比如传入2.1-2.9,includes方法是当作2进行处理。
  • 对于传入的负值整数,includes方法是当作0进行处理。
  • 对于传入的StringObjectArrayBoolean类型的值,includes方法是当作0进行处理。
  • 对于传入的Date类型的值,includes方法会自动转成对应的时间戳数值,进行检索匹配。
  • 对于Number.NaNNumber.MIN_VALUENumber.NEGATIVE_INFINITYincludes方法是当作0进行处理。
  • 对于Number.MAX_VALUENumber.POSITIVE_INFINITYincludes方法是当作超出varName.length进行处理。
使用方法 结果
oString.includes("llo",2.1) true
oString.includes("llo",2.5) true
oString.includes("llo",2.8) true
oString.includes("llo",3) false
oString.includes("llo",3.1) false
oString.includes("llo",3.5) false
oString.includes("llo",3.8) false
oString.includes("llo",oString.length) false
oString.includes("llo",-2) true
oString.includes("llo",-3) true
oString.includes("llo",true) true
oString.includes("llo",false) true
oString.includes("llo","true") true
oString.includes("llo","false") true
oString.includes("llo",oo) true
oString.includes("llo",oBool) true
oString.includes("llo",oArray) true
oString.includes("llo",oDate) false
oString.includes("llo",Number.NaN) true
oString.includes("llo",Number.MAX_VALUE) false
oString.includes("llo",Number.MIN_VALUE) true
oString.includes("llo",Number.NEGATIVE_INFINITY) true
oString.includes("llo",Number.POSITIVE_INFINITY) false

4) startsWith(searchString [, position])

使用方法 结果
oString.startsWith("hell") true
oString.startsWith("ell") false
oString.startsWith("ell",1) true

第一个参数错误示例

  • 进行匹配的时候,只要原始值是定义好的,加没加双引号都可以被正常检测出来。
  • 布尔值字符串可以判断是否包含布尔值对象。
  • 自己判断是否完全包含自己是可以正常检测的。
  • 传入空字符串的时候,startsWith方法会直接返回true
  • 对于特殊的字面量,truefalsenullundefinedNaNstartsWith方法会当作普通字符串进行完全匹配
使用方法 结果
"true".startsWith(true) true
"true".startsWith("true") true
"true".startsWith(oBool) true
"false".startsWith(false) true
"false".startsWith("false") true
"false".startsWith(oBool) false
"null".startsWith(null) true
"null".startsWith("null") true
"undefined".startsWith(undefined) true
"undefined".startsWith("undefined") true
"NaN".startsWith(NaN) true
"NaN".startsWith("NaN") true
oString.startsWith("") true
oString.startsWith(oString) true
oString.startsWith(true) false
oString.startsWith(false) false
oString.startsWith(null) false
oString.startsWith(undefined) false
oString.startsWith(NaN) false

第二个参数错误示例

  • 索引值的范围是0~varName.length-1
  • 传入的数字是向下取整的,只取数字的整数部分,举个栗子,比如传入0.1-0.9,startsWith方法是当作0进行处理。
  • 对于传入的负值整数,startsWith方法是当作0进行处理,startsWith方法是当作0进行处理,相当于就是从字符串的索引值为0的到索引值为varName.length-1中间的字符串,检索开头是不是对应的传入的字符串。
  • 对于传入的StringObjectArrayBoolean类型的值,startsWith方法是当作0进行处理,相当于就是从字符串的索引值为0的到索引值为varName.length-1中间的字符串,检索开头是不是对应的传入的字符串。
  • 对于传入的Date类型的值,startsWith方法会自动转成对应的时间戳数值,相当于就是从字符串的索引值为varName.length的到索引值为varName.length中间的字符串,检索开头是不是对应的传入的字符串。
  • 对于Number.NaNNumber.MIN_VALUENumber.NEGATIVE_INFINITYstartsWith方法是当作0进行处理,相当于就是从字符串的索引值为0的到索引值为varName.length-1中间的字符串,检索开头是不是对应的传入的字符串。
  • 对于Number.MAX_VALUENumber.POSITIVE_INFINITYstartsWith方法是当作超出varName.length进行处理,相当于就是从字符串的索引值为varName.length的到索引值为varName.length中间的字符串,检索开头是不是对应的传入的字符串。
使用方法 结果
oString.startsWith("hell",10.1) false
oString.startsWith("hell",10.5) false
oString.startsWith("hell",10.8) false
oString.startsWith("hell",11) true
oString.startsWith("hell",11.1) true
oString.startsWith("hell",11.5) true
oString.startsWith("hell",11.8) true
oString.startsWith("hell",-10) false
oString.startsWith("hell",-11) false
oString.startsWith("hell",true) false
oString.startsWith("hell",false) false
oString.startsWith("hell","true") false
oString.startsWith("hell","false") false
oString.startsWith("hell",oo) false
oString.startsWith("hell",oBool) false
oString.startsWith("hell",oArray) false
oString.startsWith("hell",oDate) true
oString.startsWith("hell",Number.NaN) false
oString.startsWith("hell",Number.MAX_VALUE) true
oString.startsWith("hell",Number.MIN_VALUE) false
oString.startsWith("hell",Number.NEGATIVE_INFINITY) false
oString.startsWith("hell",Number.POSITIVE_INFINITY) true

5) endsWith(searchString [, position])

使用方法 结果
oString.endsWith("little") true
oString.endsWith("littl") false
oString.endsWith("littl",oString.length-1) true

第一个参数错误示例

  • 进行匹配的时候,只要原始值是定义好的,加没加双引号都可以被正常检测出来。
  • 布尔值字符串可以判断是否包含布尔值对象。
  • 自己判断是否完全包含自己是可以正常检测的。
  • 传入空字符串的时候,endsWith方法会直接返回true
  • 对于特殊的字面量,truefalsenullundefinedNaNendsWith方法会当作普通字符串进行完全匹配
使用方法 结果
"true".endsWith(true) true
"true".endsWith("true") true
"true".endsWith(oBool) true
"false".endsWith(false) true
"false".endsWith("false") true
"false".endsWith(oBool) false
"null".endsWith(null) true
"null".endsWith("null") true
"undefined".endsWith(undefined) true
"undefined".endsWith("undefined") true
"NaN".endsWith(NaN) true
"NaN".endsWith("NaN") true
oString.endsWith("") true
oString.endsWith(oString) true
oString.endsWith(true) false
oString.endsWith(false) false
oString.endsWith(null) false
oString.endsWith(undefined) false
oString.endsWith(NaN) false

第二个参数错误示例

  • 索引值的范围是1~varName.length
  • 传入的数字是向下取整的,只取数字的整数部分,举个栗子,比如传入10.1-10.9,endsWith方法是当作10进行处理。
  • 对于传入的负值整数,endsWith方法是当作1进行处理,endsWith方法是当作1进行处理,相当于就是从字符串的索引值为1的到索引值为1中间的字符串,检索结尾是不是对应的传入的字符串。
  • 对于传入的StringObjectArrayBoolean类型的值,endsWith方法是当作1进行处理,相当于就是从字符串的索引值为1的到索引值为1中间的字符串,检索结尾是不是对应的传入的字符串。
  • 对于传入的Date类型的值,endsWith方法会自动转成对应的时间戳数值,相当于就是从字符串的索引值为varName.length的到索引值为1中间的字符串,检索结尾是不是对应的传入的字符串。
  • 对于Number.NaNNumber.MIN_VALUENumber.NEGATIVE_INFINITYendsWith方法是当作1进行处理,相当于就是从字符串的索引值为1的到索引值为1中间的字符串检索内容。
  • 对于Number.MAX_VALUENumber.POSITIVE_INFINITYendsWith方法是当作超出varName.length进行处理,相当于就是从字符串的索引值为varName.length的到索引值为1中间的字符串,检索结尾是不是对应的传入的字符串。
使用方法 结果
oString.endsWith("little",10.1) false
oString.endsWith("little",10.5) false
oString.endsWith("little",10.8) false
oString.endsWith("little",11) true
oString.endsWith("little",11.1) true
oString.endsWith("little",11.5) true
oString.endsWith("little",11.8) true
oString.endsWith("little",-10) false
oString.endsWith("little",-11) false
oString.endsWith("little",true) false
oString.endsWith("little",false) false
oString.endsWith("little","true") false
oString.endsWith("little","false") false
oString.endsWith("little",oo) false
oString.endsWith("little",oBool) false
oString.endsWith("little",oArray) false
oString.endsWith("little",oDate) true
oString.endsWith("little",Number.NaN) false
oString.endsWith("little",Number.MAX_VALUE) true
oString.endsWith("little",Number.MIN_VALUE) false
oString.endsWith("little",Number.NEGATIVE_INFINITY) false
oString.endsWith("little",Number.POSITIVE_INFINITY) true

6) search(regexp)


当你想要知道字符串中是否存在某个模式pattern时可使用 search,类似于正则表达式的 test 方法。当要了解更多匹配信息时,可使用 match会更慢,该方法类似于正则表达式的 exec 方法。

  • regexp该参数可以是需要在varName中检索的子串,也可以是需要检索的RegExp对象。
  • 要执行忽略大小写的检索,请追加标志i
  • 返回值varName中第一个与regexp相匹配的子串的起始位置。
  • 如果没有找到任何匹配的子串,则返回-1
  • search() 方法不执行全局匹配,它将忽略标志g
  • regexp 一个正则表达式(regular expression)对象。如果传入一个非正则表达式对象,则会使用 new RegExp(obj) 隐式地将其转换为正则表达式对象。
  • 对于字符串中包含的BMP字符,会严格匹配编码格式。
使用方法 结果
tString_2.search("  ")//两个空格 -1
tString_2.search(/\s/) 5
tString_2.search("line") 6
tString_2.search("    ")//四个空格 -1
tString_2.search("     ")//五个空格 -1
tString_2.search(" ")//一个tab缩进 13
tString_2.search(" ")//两个tab缩进 13
tString_2.search(/\t/)//一个tab缩进 13
tString_2.search(/\n/)//一个tab缩进 12
tString_2.search(/\n/)//一个tab缩进 12
oString_1.search("\uD87E\uDC04")) 2
oString_1.search("你")) -1
oString_1.normalize().search("你")) 2

错误示例

  • 进行匹配的时候,只要原始值是定义好的,没加双引号都可以被正常检测出来
  • 所有字符串检测是否包含空字符串,都会返回0
  • 所有字符串检测是否包含自己,都会返回0
  • 对于特殊的字面量,truefalsenullNaNsearch方法会当作普通字符串
  • 所有字符串检测是否包含undefined,都会返回0
  • 而且本方法不管你传入多少参数,这边只会处理传入的第一个参数值
  • 对于object这个对象,search方法会转化成[object Object]
  • 对于其他除了object之外引用类型对象,search方法会按照对象的字面量进行检索
使用方法 结果
"true".search(true) 0
"true".search("true") 0
"true".search(oBool) 0
"false".search(false) 0
"false".search("false") 0
"false".search(oBool) -1
"null".search(null) 0
"null".search("null") 0
"undefined".search(undefined) 0
"undefined".search("undefined") 0
"NaN".search(NaN) 0
"NaN".search("NaN") 0
oString.search("") 0
oString.search(oString) 0
oString.search(true) -1
oString.search(false) -1
oString.search(null) -1
oString.search(undefined) 0
oString.search(NaN) -1
oString.search("llo",10.1) 2
oString.search("llo",10.1,10.5,10.8) 2
strString.search(oo) 1
oString.search(oo) 1
tString_1.search(oo) 1
tString_2.search(oo) 1
tString_3.search(oo) 3
tString_4.search(oo) -1
oString.search(oBool) -1
oString.search(oArray) -1
oString.search(oDate) -1
oString.search(Number.NaN) -1
oString.search(Number.MAX_VALUE) -1
oString.search(Number.MIN_VALUE) -1
oString.search(Number.NEGATIVE_INFINITY) -1
oString.search(Number.POSITIVE_INFINITY) -1

7) match(regexp)

  • regexp该参数可以是需要在varName中检索的子串,也可以是需要检索的RegExp对象。
  • regexp 一个正则表达式(regular expression)对象。如果传入一个非正则表达式对象,则会使用 new RegExp(obj) 隐式地将其转换为正则表达式对象。
  • 要执行忽略大小写的检索,请追加标志i
  • 如果match()方法没有找到匹配,将返回 null。如果找到匹配,则match()方法返回一个数组,并将更新全局 RegExp 对象的属性以反映匹配结果。
  • 如果传入空格,制表符和垂直制表符等等,match() 方法的第一个参数会返回经过正则对象转化之后的值。举个栗子,如果传入" "(一个tab缩进),match()方法返回的数组第一个值是\t
  • 如果字符串中本身包含空格,制表符和垂直制表符等等,match() 方法的第三个参数的值中会将这些符号转成对应正则值。举个栗子,tString_2中包含换行和制表符,match()方法返回的数组第三个值是hello line 1\n\t\t\t\thello line 2
  • 如果没有设置全局标志 (g),数组元素0包含整个匹配,而元素1n包含任何一个子匹配。此行为与未设置全局标志时exec方法(正则表达式)(JavaScript) 的行为相同。
  • 如果未设置全局标志,则 match() 方法返回的数组有两个特性:inputindexinput属性包含整个被搜索的字符串index 属性包含了在整个被搜索字符串中匹配的子字符串的位置
  • 如果设置了全局标志,则元素0到元素n包含所有出现的匹配,可以用来统计某个内容在字符串中出现的次数。
  • 对于字符串中包含的BMP字符,会严格匹配编码格式。
使用方法 结果
tString_2.match("  ")//两个空格 null
tString_2.match(/\s/) [ ' ', index: 5, input: 'hello line 1\n\t\t\t\thello line 2' ]
tString_2.match("line") [ 'line', index: 6, input: 'hello line 1\n\t\t\t\thello line 2' ]
tString_2.match("    ")//四个空格 null
tString_2.match("     ")//五个空格 null
tString_2.match(" ")//一个tab缩进 [ '\t', index: 13, input: 'hello line 1\n\t\t\t\thello line 2' ]
tString_2.match(" ")//两个tab缩进 [ '\t\t',index: 13,input: 'hello line 1\n\t\t\t\thello line 2' ]
tString_2.match(/\t/)//一个tab缩进 [ '\t', index: 13, input: 'hello line 1\n\t\t\t\thello line 2' ]
tString_2.match(/\n/)//一个tab缩进 [ '\n', index: 12, input: 'hello line 1\n\t\t\t\thello line 2' ]
oString.match() [ '', index: 0, input: 'hello little' ]
oString.match(/o/) [ 'o', index: 4, input: 'hello little' ]
oString.match(/o/g) [ 'o', 'o' ]
oString_1.match("\uD87E\uDC04")) [ '你', index: 2, input: 'A 你 Z' ]
oString_1.match("你")) null
oString_1.normalize().match("你")) [ '你', index: 2, input: 'A 你 Z' ]

错误示例

  • 进行匹配的时候,只要原始值是定义好的,没加双引号都可以被正常检测出来。
  • 所有字符串检测是否包含空字符串,会当作匹配所有,返回的数组中的第一个值都是'',第二个值都是index:0
  • 所有字符串检测是否包含自己,都会正常返回。
  • 对于特殊的字面量,truefalsenullNaNmatch方法会当作普通字符串。
  • 除了字符串本身值包含undefined这个值,其他字符串使用match方法检测是否包含undefined,返回的数组中的第一个值都是'',第二个值都是index:0
  • 对于本身包含undefined这个值的字符串,返回的数组中第一个值是'undefined'
  • 而且本方法不管你传入多少参数,这边只会处理传入的第一个参数值。
  • 对于object这个对象,match方法会转化成[object Object],再进行匹配处理。
  • 对于其他除了object之外引用类型对象,match方法会按照对象的字面量进行检索。
使用方法 结果
"true".match(true) [ 'true', index: 0, input: 'true' ]
"true".match("true") [ 'true', index: 0, input: 'true' ]
"true".match(oBool) [ 'true', index: 0, input: 'true' ]
"false".match(false) [ 'false', index: 0, input: 'false' ]
"false".match("false") [ 'false', index: 0, input: 'false' ]
"false".match(oBool) null
"null".match(null) [ 'null', index: 0, input: 'null' ]
"null".match("null") [ 'null', index: 0, input: 'null' ]
"undefined".match(undefined) [ 'undefined', index: 0, input: 'undefined' ]
"undefined".match("undefined") [ 'undefined', index: 0, input: 'undefined' ]
"NaN".match(NaN) [ 'NaN', index: 0, input: 'NaN' ]
"NaN".match("NaN") [ 'NaN', index: 0, input: 'NaN' ]
oString.match("") [ '', index: 0, input: 'hello little' ]
oString.match(oString) [ 'hello little', index: 0, input: 'hello little' ]
oString.match(true) null
oString.match(false) null
oString.match(null) null
oString.match(undefined) [ '', index: 0, input: 'hello little' ]
oString.match(NaN) null
oString.match("llo",10.1) [ 'llo', index: 2, input: 'hello watermelon' ]
oString.match("llo",10.1,10.5,10.8) [ 'llo', index: 2, input: 'hello watermelon' ]
strString.match(oo) [ 'e', index: 1, input: 'hello watermelon' ]
oString.match(oo) [ 'e', index: 1, input: 'hello little' ]
tString_1.match(oo) [ 'e', index: 1, input: 'hello Template' ]
tString_2.match(oo) [ 'e', index: 1, input: 'hello line 1\n\t\t\t\thello line 2' ]
tString_3.match(oo) [ 't', index: 3, input: 'Fifteen is 12 and\nnot 16.' ]
tString_4.match(oo) null
oString.match(oBool) null
oString.match(oArray) null
oString.match(oDate) null
oString.match(Number.NaN) null
oString.match(Number.MAX_VALUE) null
oString.match(Number.MIN_VALUE) null
oString.match(Number.NEGATIVE_INFINITY) null
oString.match(Number.POSITIVE_INFINITY) null

本文使用 mdnice 排版