字符串和正则表达式的使用

439 阅读6分钟

字符串是一个伪数组

特点:

  1. 字符串可以遍历,但是不能使用数组的方法
  2. 字符串不能通过下标单独修改,只允许整体赋值
  3. 针对字符串的所有操作,都是返回一个新的字符串,原字符串不会修改

----------

str.length

改变length会直接作用于原数组:

  • 减小length会从数组尾部 删除元素
  • 增大length会向数组尾部添加值为undefined的元素
const arr = [0, 1, 2]
console.log(arr.length)	// 3
arr.length=2
console.log(arr)	// [0,1]
arr.length = 5
console.log(arr)	// [0,1,undefined,undefined,undefined]

----------

str.indexOf(字符,start)

(从指定下标开始),从前往后查找,返回某个指定的字符串值在字符串中首次出现的位置,没有找到则返回 -1

const str = 'Hello world'
const n = str.indexOf('o')
console.log(n)	// 4

str.lastIndexOf(字符,start)

(从指定下标开始),从后往前查找,返回某个指定的字符串值在字符串中最后出现的位置,没有找到则返回 -1

const str = 'Hello world'
const n = str.lastIndexOf('o')
console.log(n)	// 7

str.includes(字符,start)

(从指定下标开始),从前往后查找,判断字符串是否包含指定的子字符串

返回布尔值

const str = 'Hello world'
const n = str.includes('world')
console.log(n) // true

----------

str.charAt(index)

返回指定位置的字符,没找到返回空字符

const str = 'JT'
const a = str.charAt(1)
const b = str.charAt(2)
console.log(a)	// T
console.log(b)	// ''
console.log(typeof b)	// String
console.log(a + b + a)	// TT

str.fromCharCode(index)

返回指定位置字符的 Unicode 字符代码,没找到返回NaN

const str = 'HELLO WORLD'
const n = str.fromCharCode(2)
console.log(n)		// 76

String.charCodeAt(Unicode 编码)

将 一个或多个 Unicode 字符代码转对应的字符串

const str = String.fromCharCode(72,69,76,76,79)
console.log(str)		// HELLO
const str2=String.fromCharCode(25105,29233,20320)
console.log(str2)       // 我爱你

str.startsWith(字符,start)

指定索引位置开始检查字符串是否以指定字符开头

const str = 'Hello world'
const n = str.startsWith('world', 6)
console.log(n)		// true

str.endsWith(字符,length)

指定长度上检查字符串是否以指定字符结尾

const str = 'Hello world'
const m = str.endsWith('Hello', 5)
console.log(m)		// true
const n = str.endsWith('Hello', 6)
console.log(n)		// false

----------

str.concat(str2,str3...)

连接两个或多个字符串,返回一个新字符串

该方法没有改变原有字符串

const str = 'I'
const str2 = 'hate'
const str3 = 'you'
const space = ' '
console.log(str.concat(space, str2, space, str3))
// I hate you
console.log(str) // I

str.repeat(次数)

复制字符串指定次数,并将它们连接在一起返回

const str = 'abc'
console.log(str.repeat(2))	// abcabc
console.log(str) // abc

----------

str.trim()

去除字符串的头尾空格

const str = ' ab c '
console.log(str.trim())	// ab c
console.log(str) 		//  ab c 

str.slice(start,end)

从star下标到end下标(不包括end)截取字符串,返回一个新字符串

star和end为可选,不指定时,复制整个字符串

必须保证end>star且为正,否则返回空字符串

const str = '01234'
console.log(str.slice())	    // 01234
console.log(str.slice(2, 4))	// 23
console.log(str.slice(2, 5))	// 234
console.log(str.slice(2, 6))	// 234

console.log(str.slice(4, 2))	// ''
console.log(str.slice(-1, 2))	// ''

str.substr(start,length)

从star下标开始截取length长度的字符串,返回一个新字符串

star如果是负数,从后往前,-1 指字符串中最后一个字符,-2 指倒数第二个字符,以此类推

const str = '01234'
console.log(str.substr())	    // 01234
console.log(str.substr(1, 2))	// 12
console.log(str.substr(1, 4))	// 1234
console.log(str.substr(1, 5))	// 1234

console.log(str.substr(1, -1))	// ''
console.log(str.substr(-3, 2))	// 23

str.substring(index1,index2)

从index1和index2区间内(不包括它们中的最大值所对应的项)截取字符串,返回一个新字符串

指定负数下标为被当作0下标处理

const str = '01234'
console.log(str.substring())	    // 01234
console.log(str.substring(2, 4))	// 23
console.log(str.substring(2, 5))	// 234
console.log(str.substring(2, 6))	// 234

console.log(str.substring(4, 2))	// 23
console.log(str.substring(-1, 2))	// 01
console.log(str.substring(-1, -2))	// ''

----------

str.split(分割字符)

将字符串按照特定的分割符分割成数组

不指定分割符会直接逐字符分割

const str1 = 'a|b|c'
const arr1 = str1.split('|')
console.log(arr1)   //  [ 'a', 'b', 'c' ]
const str2 = 'abc'
const arr2 = str2.split('')
console.log(arr2)   //  [ 'a', 'b', 'c' ]

----------

str.toLowerCase()

把字符串转换为小写

const str1 = 'I HATE YOU'
const str2 = str1.toLowerCase()
console.log(str1) // I HATE YOU
console.log(str2) // i hate you

str.toLocaleLowerCase()根据本地主机的语言环境把字符串转换为小写

str.toUpperCase()

把字符串转换为大写

const str1 = 'i hate you'
const str2 = str1.toUpperCase()
console.log(str1) // i hate you
console.log(str2) // I HATE YOU

str.toLocaleUpperCase()根据本地主机的语言环境把字符串转换为大写

----------

str.search(字符串或regexp)

查找第一个匹配到的字符串或者正则表达式所在下标,没找到返回-1

const str = 'Mr. Blue has a blue house'
const n = str.search(/blue/i)
console.log(n)		// 4

str.match(regexp)

查找(默认第一个)匹配到的正则表达式并返回一个存放符合条件的字符串的数组,没找到返回null

const str = 'Mr. Blue has a blue house'
const n = str.match(/blue/gi)
console.log(n)		// [ 'Blue', 'blue' ]

str.replace(searchvalue,newvalue)

用一些字符替换另一些字符,或替换一个与正则表达式匹配的子串

const str = 'Mr Blue has a blue house'
const n = str.replace(/blue/gi, 'red')
console.log(n)
// Mr red has a red house

----------

RegExp正则表达式

创建正则表达式

  1. 构造函数的方式

    let regExp = new RegExp(/\d/)
    
  2. 正则字面量

    let regExp = /\d/
    
  3. 正则的使用

    /\d/.test("aaa1") // true
    

修饰符

修饰符描述
i执行对大小写不敏感的匹配。
g执行全局匹配(查找所有匹配而非在找到第一个匹配后停止)
m执行多行匹配

元字符

元字符描述
.查找单个字符,除了换行和行结束符
\w查找数字、字母及下划线
\W查找非单词字符
\d查找数字
\D查找非数字字符
\s查找空白字符
\S查找非空白字符
\b匹配单词边界
\B匹配非单词边界
\0查找 NULL 字符
\n查找换行符
\f查找换页符
\r查找回车符
\t查找制表符
\v查找垂直制表符
\xxx查找以八进制数 xxx 规定的字符
\xdd查找以十六进制数 dd 规定的字符
\uxxxx查找以十六进制数 xxxx 规定的 Unicode 字符

量词

量词描述
n+匹配任何包含至少一个 n 的字符串
n*匹配任何包含零个或多个 n 的字符串
n?匹配任何包含零个或一个 n 的字符串
n{X}匹配包含 X 个 n 的序列的字符串
n{X,}X 是一个正整数。前面的模式 n 连续出现至少 X 次时匹配
n{X,Y}X 和 Y 为正整数。前面的模式 n 连续出现至少 X 次,至多 Y 次时匹配
n$匹配任何结尾为 n 的字符串
^n匹配任何开头为 n 的字符串
?=n匹配任何其后紧接指定字符串 n 的字符串
?!n匹配任何其后没有紧接指定字符串 n 的字符串

RegExp 对象方法

RegExpObject.test(str)

检测一个字符串是否匹配某个模式

如果字符串中有匹配的值返回 true ,否则返回 false

console.log(/\d/.test('aaa1'))  // true

RegExpObject.exec(str)

检索字符串中的正则表达式的匹配

如果字符串中有匹配的值返回该匹配值,否则返回 null

console.log(/\d/.exec('aaa1'))  // 1