属性

  1. var x = "Mozilla";
  2. x.length

方法

charAt

  1. str.charAt(index)
  2. var str = "abc"
  3. str[3]; //undefined
  4. str.charAt(3) // ""

字符串是一个类似数组的对象,有length属性,可以通过数值键(0,1)取值;
str[i] 与 str.charAt(i)本质上没有区别,只是str.charAt(i)更标准而已

字符串处理

方法 描述
concat() 连接两个或更多字符串,并返回新的字符串。
split() 把字符串分割为字符串数组。
trim() 去除字符串两边的空白。
repeat() 复制字符串指定次数,并将它们连接在一起返回。
slice() 提取字符串的片断,并在新的字符串中返回被提取的部分。
substring() 提取字符串中两个指定的索引号之间的字符。
substr() 从起始索引号提取字符串中指定数目的字符。
toLowerCase() 把字符串转换为小写。
toUpperCase() 把字符串转换为大写。

concat

强烈建议使用赋值操作符(+, +=)代替 concat 方法

  1. var s1 = 'abc';
  2. var s2 = 'def';
  3. s1.concat(s2) // "abcdef"

split

  1. const str = 'The quick brown fox jumps over the lazy dog.';
  2. const words = str.split(' ');
  3. console.log(words[3]);
  4. // expected output: "fox"

trim

  1. const greeting = ' Hello world! ';
  2. console.log(greeting);
  3. // expected output: " Hello world! ";
  4. console.log(greeting.trim());
  5. // expected output: "Hello world!";

repeat

  1. "abc".repeat(0) // ""
  2. "abc".repeat(1) // "abc"
  3. "abc".repeat(2) // "abcabc"

slice

  1. str.slice(beginIndex, endIndex)
  2. var str1 = 'The morning is upon us.', // str1 的长度 length 是 23。
  3. str2 = str1.slice(1, 8),
  4. str3 = str1.slice(4, -2),
  5. str4 = str1.slice(12),
  6. str5 = str1.slice(30);
  7. console.log(str2); // 输出:he morn
  8. console.log(str3); // 输出:morning is upon u
  9. console.log(str4); // 输出:is upon us.
  10. console.log(str5); // 输出:""

substring

  1. var anyString = "Mozilla";
  2. // 输出 "Moz"
  3. console.log(anyString.substring(0,3));

padStart/padEnd

  1. 'abc'.padStart(10); // " abc"
  2. 'abc'.padStart(10, "foo"); // "foofoofabc"
  3. 'abc'.padStart(6,"123465"); // "123abc"
  4. 'abc'.padStart(8, "0"); // "00000abc"
  1. 'abc'.padEnd(10); // "abc "
  2. 'abc'.padEnd(10, "foo"); // "abcfoofoof"


字符串判断

方法 描述
includes() 查找字符串中是否包含指定的子字符串。
startsWith() 查看字符串是否以指定的子字符串开头。
endsWith() 判断当前字符串是否是以指定的子字符串结尾的(区分大小写)。
indexOf() 返回某个指定的字符串值在字符串中首次出现的位置。
lastIndexOf() 从后向前搜索字符串,并从起始位置(0)开始计算返回字符串最后出现的位置。

includes

这个方法可以帮你判断一个字符串是否包含另外一个字符串。

  1. str.includes(searchString, position)
  2. 'Blue Whale'.includes('blue'); // returns false 区分大小写

startsWith/endsWith

endsWith()方法用来判断当前字符串是否是以另外一个给定的子字符串“结尾”的,根据判断结果返回 true 或 false。

  1. str.endsWith(searchString,length)
  2. var str = "To be, or not to be, that is the question.";
  3. alert( str.endsWith("question.") ); // true
  4. alert( str.endsWith("to be") ); // false
  5. alert( str.endsWith("to be", 19) ); // true

indexOf/lastIndexOf

indexOf() 方法返回调用它的 String 对象中第一次出现的指定值的索引,从 fromIndex 处进行搜索。如果未找到该值,则返回 -1。

  1. str.indexOf(searchValue,fromIndex)

该方法将从尾到头地检索字符串 str,看它是否含有子串 searchValue。开始检索的位置在字符串的 fromIndex 处或字符串的结尾(没有指定 fromIndex 时)。如果找到一个 searchValue,则返回 searchValue 的第一个字符在 str 中的位置。str中的字符位置是从 0 开始的。

正则

方法 描述
match() 查找找到一个或多个正则表达式的匹配。
replace() 在字符串中查找匹配的子串,并替换与正则表达式匹配的子串。
replaceAll() 在字符串中查找匹配的子串,并替换与正则表达式匹配的所有子串。
search() 查找与正则表达式相匹配的值。

match

match(regExp) 方法检索返回一个字符串匹配正则表达式的结果。

  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);

matchAll(regExp) 方法返回一个包含所有匹配正则表达式的结果及分组捕获组的迭代器。

regExp必须是设置了全局模式g的形式,否则会抛出异常TypeError。

replace

  1. str.replace(regexp|substr, newSubStr|function)
  2. console.log(p.replace('dog', 'monkey'));
  3. // expected output: "The quick brown fox jumps over the lazy monkey. If the dog reacted, was it really lazy?"
  4. const regex = /Dog/i;
  5. console.log(p.replace(regex, 'ferret'));
  6. // expected output: "The quick brown fox jumps over the lazy ferret. If the dog reacted, was it really lazy?"

search

执行正则表达式和 String 对象之间的一个搜索匹配。

  1. var str = "hey JudE";
  2. var re = /[A-Z]/g;
  3. var re2 = /[.]/g;
  4. console.log(str.search(re)); // returns 4, which is the index of the first capital letter "J"
  5. console.log(str.search(re2)); // returns -1 cannot find '.' dot punctuation