indexOf()

:::info 描述:查找字符在字符串中的首次出现的下标
返回值:返回下标,如果字符串不存在则返回 -1
参数1:要被查询的字符串
参数2:开始查找的位置,默认是 0 :::

  1. "string".indexOf("i"); // 3
  2. "string".indexOf("i", 2); // 3

lastIndexOf()

:::info 描述:查找字符在字符串中的最后出现的下标,
返回值:返回下标,如果字符串不存在则返回 -1
参数1:要被查询的字符串
参数2:开始查找的位置,默认是 0 :::

  1. 'canal'.lastIndexOf('a'); // 3
  2. 'canal'.lastIndexOf('a', 2); // 1

concat()

:::info 描述:将一个或多个字符串进行合并
返回值:合并后的新字符串。
⭐️ 不会更改原字符串
参数:需要合并的字符串 :::

  1. let hello = 'Hello, '
  2. console.log(hello.concat('Kevin', '. Have a nice day.'))
  3. // Hello, Kevin. Have a nice day.

match()

:::info 描述:返回一个字符串匹配正则表达式的结果
返回值:返回与完整正则表达式匹配的所有结果
参数:正则 :::

  1. const paragraph = 'The quick brown fox jumps over the lazy dog. It barked.';
  2. const regex = /[A-Z]/g;
  3. const found = paragraph.match(regex);
  4. console.log(found); // ["T", "I"]

search()

:::info 描述:返回正则在字符串中的下标
返回值:首次匹配项的索引,如果没有则返回 -1 :::

  1. var str = "hey JudE";
  2. var re = /[A-Z]/g;
  3. var re2 = /[.]/g;
  4. console.log(str.search(re)); // 4
  5. console.log(str.search(re2)); // -1

replace

:::info 描述:替换字符串中的字符
返回值:返回替换后的新字符串
⭐️ 不会更改原字符串
参数1:要替换的字符 | 正则
参数2:新字符 | function :::

  1. const p = 'The quick brown fox jumps over the lazy dog.';
  2. console.log(p.replace('dog', 'monkey'));
  3. // The quick brown fox jumps over the lazy monkey.
  4. const regex = /Dog/i;
  5. console.log(p.replace(regex, 'ferret'));
  6. // The quick brown fox jumps over the lazy ferret.

当指定参数2是一个函数的时,当匹配执行后,该函数就会执行。 函数的返回值作为替换字符串。
具体细节(和String.prototype.replace()联合的案例):
RegExp 正则

split()

:::info 描述:按照指定的分隔符规则将字符串分割成为数组
参数:分隔符规则
返回:分隔后的数组 :::

  1. const str = 'The quick brown fox jumps over the lazy dog.';
  2. console.log(str.split(' '));
  3. // ['The', 'quick', 'brown', 'fox', 'jumps', 'over', 'the', 'lazy', 'dog.']

这和Array.prototype.join()是相反的:

  1. let arr = ['The', 'quick', 'brown', 'fox', 'jumps', 'over', 'the', 'lazy', 'dog.'];
  2. console.log(arr.join(" "));
  3. // The quick brown fox jumps over the lazy dog.

slice()

:::info 描述:提取某个字符串的一部分,
返回值:返回一个提取到的新字符串
⭐️ 不会改动原字符串
参数1: 开始提取的下标,容许为负数
参数2: 结束提取的下标(返回的字符不包括结束下标处的字符),如果没有参数2则一直提取到末尾,容许为负数 :::

  1. var str1 = 'The morning is upon us.'
  2. console.log(str1.slice(1, 8)); // he morn
  3. console.log(str1.slice(4, -2)); // morning is upon u
  4. console.log(str1.slice(12)); // is upon us.
  5. console.log(str1.slice(30)); // ""
  1. var str = 'The morning is upon us.';
  2. str.slice(-3); // 'us.'
  3. str.slice(-3, -1); // 'us'
  4. str.slice(0, -1); // 'The morning is upon us'

substring()

:::info 描述:返回一个字符串在开始索引到结束索引之间的一个字符串
返回值:之间的新字符串
参数1: 开始截取的索引
参数2: 结束截取的索引(不包括)

  • 如果参数1等于参数2,返回一个空字符串。
  • 如果省略参数2 ,提取字符一直到字符串末尾。
  • 如果任一参数小于 0 或为NaN,则被当作 0。
  • 如果任一参数大于字符串的长度,则被当作字符串的长度
  • 如果参数1大于参数2,执行效果就像两个参数调换了一样 ::: ```javascript var anyString = “Mozilla”;

// 输出 “Moz” console.log(anyString.substring(0,3)); console.log(anyString.substring(3,0)); console.log(anyString.substring(3,-3)); console.log(anyString.substring(3,NaN)); console.log(anyString.substring(-2,3)); console.log(anyString.substring(NaN,3));

// 输出 “lla” console.log(anyString.substring(4,7)); console.log(anyString.substring(7,4));

// 输出 “” console.log(anyString.substring(4,4));

// 输出 “Mozill” console.log(anyString.substring(0,6));

// 输出 “Mozilla” console.log(anyString.substring(0,7)); console.log(anyString.substring(0,10));

  1. <a name="Iqy4E"></a>
  2. ## toLowerCase() / toUpperCase()
  3. :::info
  4. 描述:将字符串转为小/大写形式<br />返回值:转换后的新字符串
  5. :::
  6. ```javascript
  7. console.log('中文简体 zh-CN || zh-Hans'.toLowerCase()); // 中文简体 zh-cn || zh-hans
  8. console.log( "ALPHABET".toLowerCase()); // "alphabet"
  1. console.log('alphabet'.toUpperCase()); // 'ALPHABET'

trim() / trimStart() / trimEnd()

:::info 描述:删除字符串的两端/之前/之后的空白字符
返回:删除空白字符之后的新字符串 :::

  1. var orig = ' foo ';
  2. console.log(orig.trim()); // 'foo'
  1. var orig = ' foo ';
  2. console.log(orig.trimStart()); // 'foo '
  3. console.log(orig.trimEnd()); // ' foo'