From 01b0e12ba27b5069248fd09235e9a7143915ee30 Mon Sep 17 00:00:00 2001 From: Irvin Date: Wed, 16 Feb 2022 02:02:49 +0800 Subject: remove `notranslate` class in zh-CN --- .../global_objects/string/charcodeat/index.html | 8 ++++---- .../global_objects/string/concat/index.html | 4 ++-- .../global_objects/string/endswith/index.html | 6 +++--- .../global_objects/string/fromcharcode/index.html | 6 +++--- .../global_objects/string/includes/index.html | 8 ++++---- .../global_objects/string/indexof/index.html | 18 +++++++++--------- .../global_objects/string/localecompare/index.html | 10 +++++----- .../global_objects/string/match/index.html | 10 +++++----- .../global_objects/string/matchall/index.html | 14 +++++++------- .../global_objects/string/padend/index.html | 6 +++--- .../global_objects/string/padstart/index.html | 6 +++--- .../global_objects/string/repeat/index.html | 6 +++--- .../global_objects/string/replace/index.html | 22 +++++++++++----------- .../global_objects/string/replaceall/index.html | 8 ++++---- .../global_objects/string/search/index.html | 4 ++-- .../global_objects/string/startswith/index.html | 6 +++--- .../global_objects/string/substring/index.html | 10 +++++----- .../global_objects/string/tolowercase/index.html | 4 ++-- .../global_objects/string/touppercase/index.html | 6 +++--- .../global_objects/string/trim/index.html | 6 +++--- .../global_objects/string/valueof/index.html | 4 ++-- 21 files changed, 86 insertions(+), 86 deletions(-) (limited to 'files/zh-cn/web/javascript/reference/global_objects/string') diff --git a/files/zh-cn/web/javascript/reference/global_objects/string/charcodeat/index.html b/files/zh-cn/web/javascript/reference/global_objects/string/charcodeat/index.html index 8c683db768..e0c7b1168f 100644 --- a/files/zh-cn/web/javascript/reference/global_objects/string/charcodeat/index.html +++ b/files/zh-cn/web/javascript/reference/global_objects/string/charcodeat/index.html @@ -22,7 +22,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/String/charCodeAt

语法

-
str.charCodeAt(index)
+
str.charCodeAt(index)

参数

@@ -55,7 +55,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/String/charCodeAt

下例介绍了不同索引情况下返回的 Unicode 值:

-
"ABC".charCodeAt(0) // returns 65:"A"
+
"ABC".charCodeAt(0) // returns 65:"A"
 
 "ABC".charCodeAt(1) // returns 66:"B"
 
@@ -67,7 +67,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/String/charCodeAt
 
 

这段代码可以被用在 for 循环和其他类似语句中,当在指定引索之前不确定是否有非BMP字符存在时。

-
function fixedCharCodeAt (str, idx) {
+
function fixedCharCodeAt (str, idx) {
     // ex. fixedCharCodeAt ('\uD800\uDC00', 0); // 65536
     // ex. fixedCharCodeAt ('\uD800\uDC00', 1); // false
     idx = idx || 0;
@@ -98,7 +98,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/String/charCodeAt
 
 

使用 charCodeAt() 修复字符串中出现的已知的非BMP字符

-
function knownCharCodeAt (str, idx) {
+
function knownCharCodeAt (str, idx) {
     str += '';
     var code,
         end = str.length;
diff --git a/files/zh-cn/web/javascript/reference/global_objects/string/concat/index.html b/files/zh-cn/web/javascript/reference/global_objects/string/concat/index.html
index ee2d8dd06d..7b53403ac2 100644
--- a/files/zh-cn/web/javascript/reference/global_objects/string/concat/index.html
+++ b/files/zh-cn/web/javascript/reference/global_objects/string/concat/index.html
@@ -14,7 +14,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/String/concat
 
 

语法

-
str.concat(str2, [, ...strN])
+
str.concat(str2, [, ...strN])

参数

@@ -43,7 +43,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/String/concat

下面的例子演示如何将多个字符串与原字符串合并为一个新字符串

-
let hello = 'Hello, '
+
let hello = 'Hello, '
 console.log(hello.concat('Kevin', '. Have a nice day.'))
 // Hello, Kevin. Have a nice day.
 
diff --git a/files/zh-cn/web/javascript/reference/global_objects/string/endswith/index.html b/files/zh-cn/web/javascript/reference/global_objects/string/endswith/index.html
index fb6728a8a0..dd5597701e 100644
--- a/files/zh-cn/web/javascript/reference/global_objects/string/endswith/index.html
+++ b/files/zh-cn/web/javascript/reference/global_objects/string/endswith/index.html
@@ -23,7 +23,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/String/endsWith
 
 

语法

-
str.endsWith(searchString[, length])
+
str.endsWith(searchString[, length])

参数

@@ -46,7 +46,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/String/endsWith

这个方法已经加入到 ECMAScript 6 标准当中,但是可能还没有在所有的  JavaScript 实现中可用。然而,你可以通过如下的代码片段扩展 String.prototype.endsWith() 实现兼容:

-
if (!String.prototype.endsWith) {
+
if (!String.prototype.endsWith) {
 	String.prototype.endsWith = function(search, this_len) {
 		if (this_len === undefined || this_len > this.length) {
 			this_len = this.length;
@@ -60,7 +60,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/String/endsWith
 
 

使用 endsWith()

-
var str = "To be, or not to be, that is the question.";
+
var str = "To be, or not to be, that is the question.";
 
 alert( str.endsWith("question.") );  // true
 alert( str.endsWith("to be") );      // false
diff --git a/files/zh-cn/web/javascript/reference/global_objects/string/fromcharcode/index.html b/files/zh-cn/web/javascript/reference/global_objects/string/fromcharcode/index.html
index fd399adab7..3ac0a6a122 100644
--- a/files/zh-cn/web/javascript/reference/global_objects/string/fromcharcode/index.html
+++ b/files/zh-cn/web/javascript/reference/global_objects/string/fromcharcode/index.html
@@ -16,7 +16,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/String/fromCharCode
 
 

语法

-
String.fromCharCode(num1[, ...[, numN]])
+
String.fromCharCode(num1[, ...[, numN]])

参数

@@ -49,7 +49,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/String/fromCharCode

在 UTF-16 中,BMP 字符使用一个代码单元:

-
String.fromCharCode(65, 66, 67);   // 返回 "ABC"
+
String.fromCharCode(65, 66, 67);   // 返回 "ABC"
 String.fromCharCode(0x2014);       // 返回 "—"
 String.fromCharCode(0x12014);      // 也是返回 "—"; 数字 1 被剔除并忽略
 String.fromCharCode(8212);         // 也是返回 "—"; 8212 是 0x2014 的十进制表示
@@ -58,7 +58,7 @@ String.fromCharCode(8212);         // 也是返回 "—"; 8212 是 0x2014 的十
 

完整的 UTF 16 表格.
在 UTF-16 中,补充字符需要两个代码单元(即一个代理对):

-
String.fromCharCode(0xD83C, 0xDF03); // Code Point U+1F303 "Night with
+
String.fromCharCode(0xD83C, 0xDF03); // Code Point U+1F303 "Night with
 String.fromCharCode(55356, 57091);   // Stars" == "\uD83C\uDF03"
 
 String.fromCharCode(0xD834, 0xDF06, 0x61, 0xD834, 0xDF07); // "\uD834\uDF06a\uD834\uDF07"
diff --git a/files/zh-cn/web/javascript/reference/global_objects/string/includes/index.html b/files/zh-cn/web/javascript/reference/global_objects/string/includes/index.html
index 83ced4b38a..672a34166f 100644
--- a/files/zh-cn/web/javascript/reference/global_objects/string/includes/index.html
+++ b/files/zh-cn/web/javascript/reference/global_objects/string/includes/index.html
@@ -16,7 +16,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/String/includes
 
 

语法

-
str.includes(searchString[, position])
+
str.includes(searchString[, position])

参数

@@ -39,13 +39,13 @@ translation_of: Web/JavaScript/Reference/Global_Objects/String/includes

includes() 方法是区分大小写的。例如,下面的表达式会返回 false

-
'Blue Whale'.includes('blue'); // returns false
+
'Blue Whale'.includes('blue'); // returns false

兼容补丁

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

-
if (!String.prototype.includes) {
+
if (!String.prototype.includes) {
   String.prototype.includes = function(search, start) {
     'use strict';
     if (typeof start !== 'number') {
@@ -64,7 +64,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/String/includes
 
 

使用 includes()

-
var str = 'To be, or not to be, that is the question.';
+
var str = 'To be, or not to be, that is the question.';
 
 console.log(str.includes('To be'));       // true
 console.log(str.includes('question'));    // true
diff --git a/files/zh-cn/web/javascript/reference/global_objects/string/indexof/index.html b/files/zh-cn/web/javascript/reference/global_objects/string/indexof/index.html
index 21434132e5..80213be40f 100644
--- a/files/zh-cn/web/javascript/reference/global_objects/string/indexof/index.html
+++ b/files/zh-cn/web/javascript/reference/global_objects/string/indexof/index.html
@@ -22,7 +22,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/String/indexOf
 
 

语法

-
str.indexOf(searchValue [, fromIndex])
+
str.indexOf(searchValue [, fromIndex])

参数

@@ -43,14 +43,14 @@ translation_of: Web/JavaScript/Reference/Global_Objects/String/indexOf

若被查找的字符串 searchValue 是一个空字符串,将会产生“奇怪”的结果。如果 fromIndex 值为空,或者 fromIndex 值小于被查找的字符串的长度,返回值和以下的 fromIndex 值一样:

-
'hello world'.indexOf('') // 返回 0
+
'hello world'.indexOf('') // 返回 0
 'hello world'.indexOf('', 0) // 返回 0
 'hello world'.indexOf('', 3) // 返回 3
 'hello world'.indexOf('', 8) // 返回 8

另外,如果 fromIndex 值大于等于字符串的长度,将会直接返回字符串的长度(str.length):

-
'hello world'.indexOf('', 11) // 返回 11
+
'hello world'.indexOf('', 11) // 返回 11
 'hello world'.indexOf('', 13) // 返回 11
 'hello world'.indexOf('', 22) // 返回 11
@@ -60,7 +60,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/String/indexOf

字符串中的字符被从左向右索引。第一个字符的索引(index)是 0,变量名为 stringName 的字符串的最后一个字符的索引是 stringName.length - 1 。

-
"Blue Whale".indexOf("Blue")       // 返回 0
+
"Blue Whale".indexOf("Blue")       // 返回 0
 "Blue Whale".indexOf("Blute")      // 返回 -1
 "Blue Whale".indexOf("Whale", 0)   // 返回 5
 "Blue Whale".indexOf("Whale", 5)   // 返回 5
@@ -71,14 +71,14 @@ translation_of: Web/JavaScript/Reference/Global_Objects/String/indexOf
 
 

indexOf 方法是区分大小写的。例如,下面的表达式将返回 -1

-
"Blue Whale".indexOf("blue")      // 返回 -1
+
"Blue Whale".indexOf("blue")      // 返回 -1
 

检测是否存在某字符串

注意 0 并不会被当成 true-1 不会被当成 false 。所以当检测某个字符串是否存在于另一个字符串中时,可使用下面的方法:

-
'Blue Whale'.indexOf('Blue') !== -1    // true
+
'Blue Whale'.indexOf('Blue') !== -1    // true
 'Blue Whale'.indexOf('Bloe') !== -1    // false
 ~('Blue Whale'.indexOf('Bloe'))        // 0, 这是一种错误用法
@@ -88,7 +88,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/String/indexOf

下例使用 indexOf()lastIndexOf() 方法定位字符串中 "Brave new world" 的值。

-
var anyString = "Brave new world";
+
var anyString = "Brave new world";
 
 console.log("The index of the first w from the beginning is " + anyString.indexOf("w"));
 // logs 8
@@ -105,7 +105,7 @@ console.log("The index of 'new' from the end is " + anyString.lastIndexOf("new")
 
 

下例定义了两个字符串变量。两个变量包含相同的字符串,除了第二个字符串中的某些字符为大写。第一个 log 方法输出 19。但是由于 indexOf 方法区分大小写,因此不会在 myCapString 中发现字符串 “cheddar",所以,第二个 log 方法会输出 -1。

-
var myString    = "brie, pepper jack, cheddar";
+
var myString    = "brie, pepper jack, cheddar";
 var myCapString = "Brie, Pepper Jack, Cheddar";
 
 console.log('myString.indexOf("cheddar") is ' + myString.indexOf("cheddar"));
@@ -117,7 +117,7 @@ console.log('myCapString.indexOf("cheddar") is ' + myCapString.indexOf("cheddar"
 
 

在下例中,设置了 count 来记录字母 e 在字符串 str 中出现的次数:

-
// 翻译:生存还是毁灭?这是个问题。(莎士比亚《哈姆雷特》)
+
// 翻译:生存还是毁灭?这是个问题。(莎士比亚《哈姆雷特》)
 var str = 'To be, or not to be, that is the question.';
 var count = 0;
 var pos = str.indexOf('e');
diff --git a/files/zh-cn/web/javascript/reference/global_objects/string/localecompare/index.html b/files/zh-cn/web/javascript/reference/global_objects/string/localecompare/index.html
index 1bdd45c95e..bf0e8ad0fb 100644
--- a/files/zh-cn/web/javascript/reference/global_objects/string/localecompare/index.html
+++ b/files/zh-cn/web/javascript/reference/global_objects/string/localecompare/index.html
@@ -19,7 +19,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/String/localeCompare
 
 

语法

-
referenceStr.localeCompare(compareString[, locales[, options]])
+
referenceStr.localeCompare(compareString[, locales[, options]])

参数

@@ -90,7 +90,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/String/localeCompare

使用 localeCompare()

-
// The letter "a" is before "c" yielding a negative value
+
// The letter "a" is before "c" yielding a negative value
 'a'.localeCompare('c');
 // -2 or -1 (or some other negative value)
 
@@ -107,7 +107,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/String/localeCompare
 
 

locales 和 options 参数还没有被所有浏览器所支持。检查是否被支持, 使用 "i" 参数 (a requirement that illegal language tags are rejected) 判断是否有异常 {{jsxref("RangeError")}}抛出:

-
function localeCompareSupportsLocales() {
+
function localeCompareSupportsLocales() {
   try {
     'foo'.localeCompare​('bar', 'i');
   } catch (e) {
@@ -121,7 +121,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/String/localeCompare
 
 

在不同的语言下 localeCompare() 所提供的结果是不一致的。 为了能让用户得到正确的比较值, 通过使用 locales 参数来提供要比较的语言 (and possibly some fallback languages) :

-
console.log('ä'.localeCompare('z', 'de')); // a negative value: in German, ä sorts with a
+
console.log('ä'.localeCompare('z', 'de')); // a negative value: in German, ä sorts with a
 console.log('ä'.localeCompare('z', 'sv')); // a positive value: in Swedish, ä sorts after z
 
@@ -129,7 +129,7 @@ console.log('ä'.localeCompare('z', 'sv')); // a positive value: in Swedish, ä

localeCompare() 所提供的结果可以通过 options 参数来制定:

-
// in German, ä has a as the base letter
+
// in German, ä has a as the base letter
 console.log('ä'.localeCompare('a', 'de', { sensitivity: 'base' })); // 0
 
 // in Swedish, ä and a are separate base letters
diff --git a/files/zh-cn/web/javascript/reference/global_objects/string/match/index.html b/files/zh-cn/web/javascript/reference/global_objects/string/match/index.html
index cc979f7ffe..5f5b701e63 100644
--- a/files/zh-cn/web/javascript/reference/global_objects/string/match/index.html
+++ b/files/zh-cn/web/javascript/reference/global_objects/string/match/index.html
@@ -14,7 +14,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/String/match
 
 

语法

-
str.match(regexp)
+
str.match(regexp)

参数

@@ -62,7 +62,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/String/match

在下例中,使用 match 查找 "Chapter" 紧跟着 1 个或多个数值字符,再紧跟着一个小数点和数值字符 0 次或多次。正则表达式包含 i 标志,因此大小写会被忽略。

-
var str = 'For more information, see Chapter 3.4.5.1';
+
var str = 'For more information, see Chapter 3.4.5.1';
 var re = /see (chapter \d+(\.\d)*)/i;
 var found = str.match(re);
 
@@ -84,7 +84,7 @@ console.log(found);
 
 

下例展示了 match 使用 global 和 ignore case 标志。A-E、a-e 的所有字母将会作为一个数组的元素返回。

-
var str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
+
var str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
 var regexp = /[A-E]/gi;
 var matches_array = str.match(regexp);
 
@@ -93,7 +93,7 @@ console.log(matches_array);
 
 

使用match(),不传参数

-
var str = "Nothing will come of nothing.";
+
var str = "Nothing will come of nothing.";
 
 str.match();   // returns [""]
@@ -101,7 +101,7 @@ str.match(); // returns [""]

当参数是一个字符串或一个数字,它会使用new RegExp(obj)来隐式转换成一个 {{jsxref("RegExp")}}。如果它是一个有正号的正数,RegExp() 方法将忽略正号。

-
var str1 = "NaN means not a number. Infinity contains -Infinity and +Infinity in JavaScript.",
+
var str1 = "NaN means not a number. Infinity contains -Infinity and +Infinity in JavaScript.",
     str2 = "My grandfather is 65 years old and My grandmother is 63 years old.",
     str3 = "The contract was declared null and void.";
 str1.match("number");   // "number" 是字符串。返回["number"]
diff --git a/files/zh-cn/web/javascript/reference/global_objects/string/matchall/index.html b/files/zh-cn/web/javascript/reference/global_objects/string/matchall/index.html
index f2344f6f79..d11d23265e 100644
--- a/files/zh-cn/web/javascript/reference/global_objects/string/matchall/index.html
+++ b/files/zh-cn/web/javascript/reference/global_objects/string/matchall/index.html
@@ -20,7 +20,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/String/matchAll
 
 

语法

-
str.matchAll(regexp)
+
str.matchAll(regexp)

参数

@@ -42,7 +42,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/String/matchAll

matchAll 出现之前,通过在循环中调用 regexp.exec() 来获取所有匹配项信息(regexp 需使用 /g 标志):

-
const regexp = RegExp('foo[a-z]*','g');
+
const regexp = RegExp('foo[a-z]*','g');
 const str = 'table football, foosball';
 let match;
 
@@ -54,7 +54,7 @@ while ((match = regexp.exec(str)) !== null) {
 
 

如果使用 matchAll ,就可以不必使用 while 循环加 exec 方式(且正则表达式需使用 /g 标志)。使用 matchAll 会得到一个迭代器的返回值,配合 for...of, array spread, 或者 {{jsxref("Array.from()")}} 可以更方便实现功能:

-
const regexp = RegExp('foo[a-z]*','g');
+
const regexp = RegExp('foo[a-z]*','g');
 const str = 'table football, foosball';
 const matches = str.matchAll(regexp);
 
@@ -71,14 +71,14 @@ Array.from(str.matchAll(regexp), m => m[0]);
 
 

如果没有 /g 标志,matchAll 会抛出异常。

-
const regexp = RegExp('[a-c]','');
+
const regexp = RegExp('[a-c]','');
 const str = 'abc';
 Array.from(str.matchAll(regexp), m => m[0]);
 // TypeError: String.prototype.matchAll called with a non-global RegExp argument

matchAll 内部做了一个 regexp 的复制,所以不像 regexp.execlastIndex 在字符串扫描时不会改变。

-
const regexp = RegExp('[a-c]','g');
+
const regexp = RegExp('[a-c]','g');
 regexp.lastIndex = 1;
 const str = 'abc';
 Array.from(str.matchAll(regexp), m => `${regexp.lastIndex} ${m[0]}`);
@@ -89,7 +89,7 @@ Array.from(str.matchAll(regexp), m => `${regexp.lastIndex} ${m[0]}`);
 
 

matchAll 的另外一个亮点是更好地获取捕获组。因为当使用 match() 和 /g 标志方式获取匹配信息时,捕获组会被忽略:

-
var regexp = /t(e)(st(\d?))/g;
+
var regexp = /t(e)(st(\d?))/g;
 var str = 'test1test2';
 
 str.match(regexp);
@@ -97,7 +97,7 @@ str.match(regexp);
 
 

使用 matchAll 可以通过如下方式获取分组捕获:

-
let array = [...str.matchAll(regexp)];
+
let array = [...str.matchAll(regexp)];
 
 array[0];
 // ['test1', 'e', 'st1', '1', index: 0, input: 'test1test2', length: 4]
diff --git a/files/zh-cn/web/javascript/reference/global_objects/string/padend/index.html b/files/zh-cn/web/javascript/reference/global_objects/string/padend/index.html
index ca1f5d8515..e73b847c5b 100644
--- a/files/zh-cn/web/javascript/reference/global_objects/string/padend/index.html
+++ b/files/zh-cn/web/javascript/reference/global_objects/string/padend/index.html
@@ -18,7 +18,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/String/padEnd
 
 

语法

-
str.padEnd(targetLength [, padString])
+
str.padEnd(targetLength [, padString])

参数

@@ -35,7 +35,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/String/padEnd

示例

-
'abc'.padEnd(10);          // "abc       "
+
'abc'.padEnd(10);          // "abc       "
 'abc'.padEnd(10, "foo");   // "abcfoofoof"
 'abc'.padEnd(6, "123456"); // "abc123"
 'abc'.padEnd(1);           // "abc"
@@ -45,7 +45,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/String/padEnd
 
 

如果原生环境不支持该方法,在其他代码之前先运行下面的代码,将创建 String.prototype.padEnd() 方法。

-
// https://github.com/uxitten/polyfill/blob/master/string.polyfill.js
+
// https://github.com/uxitten/polyfill/blob/master/string.polyfill.js
 // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/padEnd
 if (!String.prototype.padEnd) {
     String.prototype.padEnd = function padEnd(targetLength,padString) {
diff --git a/files/zh-cn/web/javascript/reference/global_objects/string/padstart/index.html b/files/zh-cn/web/javascript/reference/global_objects/string/padstart/index.html
index 8596df0ae3..69aff06b94 100644
--- a/files/zh-cn/web/javascript/reference/global_objects/string/padstart/index.html
+++ b/files/zh-cn/web/javascript/reference/global_objects/string/padstart/index.html
@@ -20,7 +20,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/String/padStart
 
 

语法

-
str.padStart(targetLength [, padString])
+
str.padStart(targetLength [, padString])

参数

@@ -37,7 +37,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/String/padStart

示例

-
'abc'.padStart(10);         // "       abc"
+
'abc'.padStart(10);         // "       abc"
 'abc'.padStart(10, "foo");  // "foofoofabc"
 'abc'.padStart(6,"123465"); // "123abc"
 'abc'.padStart(8, "0");     // "00000abc"
@@ -47,7 +47,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/String/padStart
 
 

如果原生环境不支持该方法,在其他代码之前先运行下面的代码,将创建 String.prototype.padStart() 方法。

-
// https://github.com/uxitten/polyfill/blob/master/string.polyfill.js
+
// https://github.com/uxitten/polyfill/blob/master/string.polyfill.js
 // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/padStart
 if (!String.prototype.padStart) {
     String.prototype.padStart = function padStart(targetLength,padString) {
diff --git a/files/zh-cn/web/javascript/reference/global_objects/string/repeat/index.html b/files/zh-cn/web/javascript/reference/global_objects/string/repeat/index.html
index b880c16b92..c0ee77fe21 100644
--- a/files/zh-cn/web/javascript/reference/global_objects/string/repeat/index.html
+++ b/files/zh-cn/web/javascript/reference/global_objects/string/repeat/index.html
@@ -20,7 +20,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/String/repeat
 
 

语法

-
str.repeat(count)
+
str.repeat(count)

参数

@@ -44,7 +44,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/String/repeat

此方法已添加到 ECMAScript 2015 规范中,并且可能尚未在所有 JavaScript 实现中可用。然而,你可以使用以下代码段对 String.prototype.repeat() 进行填充:

-
if (!String.prototype.repeat) {
+
if (!String.prototype.repeat) {
   String.prototype.repeat = function(count) {
     'use strict';
     if (this == null) {
@@ -88,7 +88,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/String/repeat
 
 

示例

-
"abc".repeat(-1)     // RangeError: repeat count must be positive and less than inifinity
+
"abc".repeat(-1)     // RangeError: repeat count must be positive and less than inifinity
 "abc".repeat(0)      // ""
 "abc".repeat(1)      // "abc"
 "abc".repeat(2)      // "abcabc"
diff --git a/files/zh-cn/web/javascript/reference/global_objects/string/replace/index.html b/files/zh-cn/web/javascript/reference/global_objects/string/replace/index.html
index 49f27efe4b..5991e8fa0a 100644
--- a/files/zh-cn/web/javascript/reference/global_objects/string/replace/index.html
+++ b/files/zh-cn/web/javascript/reference/global_objects/string/replace/index.html
@@ -21,7 +21,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/String/replace
 
 

语法

-
str.replace(regexp|substr, newSubStr|function)
+
str.replace(regexp|substr, newSubStr|function)

参数

@@ -137,7 +137,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/String/replace

下面的例子将会使 newString 变成 'abc - 12345 - #$*%'

-
function replacer(match, p1, p2, p3, offset, string) {
+
function replacer(match, p1, p2, p3, offset, string) {
   // p1 is nondigits, p2 digits, and p3 non-alphanumerics
   return [p1, p2, p3].join(' - ');
 }
@@ -151,7 +151,7 @@ console.log(newString);  // abc - 12345 - #$*%
 
 

在下面的例子中,replace() 中使用了正则表达式及忽略大小写标示。

-
var str = 'Twas the night before Xmas...';
+
var str = 'Twas the night before Xmas...';
 var newstr = str.replace(/xmas/i, 'Christmas');
 console.log(newstr);  // Twas the night before Christmas...
 
@@ -160,7 +160,7 @@ console.log(newstr); // Twas the night before Christmas...

下面的例子中,正则表达式包含有全局替换(g)和忽略大小写(i)的选项,这使得replace方法用'oranges'替换掉了所有出现的"apples".

-
var re = /apples/gi;
+
var re = /apples/gi;
 var str = "Apples are round, and apples are juicy.";
 var newstr = str.replace(re, "oranges");
 
@@ -172,7 +172,7 @@ console.log(newstr);
 
 

下面的例子演示了如何交换一个字符串中两个单词的位置,这个脚本使用$1 和 $2 代替替换文本。

-
var re = /(\w+)\s(\w+)/;
+
var re = /(\w+)\s(\w+)/;
 var str = "John Smith";
 var newstr = str.replace(re, "$2, $1");
 // Smith, John
@@ -185,7 +185,7 @@ console.log(newstr);
 
 

在返回前,替换函数允许匹配片段作为参数,并且将它和连字符进行连接作为新的片段。

-
function styleHyphenFormat(propertyName) {
+
function styleHyphenFormat(propertyName) {
   function upperToHyphenLower(match) {
     return '-' + match.toLowerCase();
   }
@@ -197,7 +197,7 @@ console.log(newstr);
 
 

因为我们想在最终的替换中进一步转变匹配结果,所以我们必须使用一个函数。这迫使我们在使用{{jsxref("String.prototype.toLowerCase()", "toLowerCase()")}}方法前进行评估。如果我们尝试不用一个函数进行匹配,那么使用{{jsxref("String.prototype.toLowerCase()", "toLowerCase()")}} 方法将不会有效。

-
var newString = propertyName.replace(/[A-Z]/g, '-' + '$&'.toLowerCase());  // won't work
+
var newString = propertyName.replace(/[A-Z]/g, '-' + '$&'.toLowerCase());  // won't work
 

这是因为 '$&'.toLowerCase() 会先被解析成字符串字面量(这会导致相同的'$&')而不是当作一个模式。

@@ -208,7 +208,7 @@ console.log(newstr);

正则表达式test检查任何数字是否以 F 结尾。华氏温度通过第二个参数p1进入函数。这个函数基于华氏温度作为字符串传递给f2c函数设置成摄氏温度。然后f2c()返回摄氏温度。这个函数与Perl的 s///e 标志相似。

-
function f2c(x)
+
function f2c(x)
 {
   function convert(str, p1, offset, s)
   {
@@ -227,7 +227,7 @@ console.log(newstr);
 

输入:
一个由 x,- 和 _ 组成的字符串。

-
x-x_
+
x-x_
 
 ---x---x---x---
 
@@ -240,7 +240,7 @@ _x_x___x___x___
 
 

一个数组对象。'x' 产生一个 'on' 状态,'-'(连接符)产生一个 'off' 状态,而 '_' (下划线)表示 'on' 状态的长度。

-
[
+
[
   { on: true, length: 1 },
   { on: false, length: 1 },
   { on: true, length: 2 }
@@ -249,7 +249,7 @@ _x_x___x___x___
 
 

代码片段:

-
var str = 'x-x_';
+
var str = 'x-x_';
 var retArr = [];
 str.replace(/(x_*)|(-)/g, function(match, p1, p2) {
   if (p1) { retArr.push({ on: true, length: p1.length }); }
diff --git a/files/zh-cn/web/javascript/reference/global_objects/string/replaceall/index.html b/files/zh-cn/web/javascript/reference/global_objects/string/replaceall/index.html
index cbd76d71b7..c9d892e3f7 100644
--- a/files/zh-cn/web/javascript/reference/global_objects/string/replaceall/index.html
+++ b/files/zh-cn/web/javascript/reference/global_objects/string/replaceall/index.html
@@ -19,7 +19,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/String/replaceAll
 
 

语法

-
const newStr = str.replaceAll(regexp|substr, newSubstr|function)
+
const newStr = str.replaceAll(regexp|substr, newSubstr|function)

当使用一个 `regex`时,您必须设置全局(“ g”)标志,
@@ -123,20 +123,20 @@ translation_of: Web/JavaScript/Reference/Global_Objects/String/replaceAll

使用 replaceAll

-
'aabbcc'.replaceAll('b', '.');
+
'aabbcc'.replaceAll('b', '.');
 // 'aa..cc'

非全局 regex 抛出

使用正则表达式搜索值时,它必须是全局的。这将行不通:

-
'aabbcc'.replaceAll(/b/, '.');
+
'aabbcc'.replaceAll(/b/, '.');
 TypeError: replaceAll must be called with a global RegExp
 

这将可以正常运行:

-
'aabbcc'.replaceAll(/b/g, '.');
+
'aabbcc'.replaceAll(/b/g, '.');
 "aa..cc"
 
diff --git a/files/zh-cn/web/javascript/reference/global_objects/string/search/index.html b/files/zh-cn/web/javascript/reference/global_objects/string/search/index.html index 06c7e214fd..35a75549c2 100644 --- a/files/zh-cn/web/javascript/reference/global_objects/string/search/index.html +++ b/files/zh-cn/web/javascript/reference/global_objects/string/search/index.html @@ -20,7 +20,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/String/search

语法

-
str.search(regexp)
+
str.search(regexp)

参数

@@ -44,7 +44,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/String/search

下面的例子中用两个不同的正则表达式对同一个字符串执行搜索匹配,得到一个成功匹配(正数返回值)和一个失败匹配(-1)。

-
var str = "hey JudE";
+
var str = "hey JudE";
 var re = /[A-Z]/g;
 var re2 = /[.]/g;
 console.log(str.search(re)); // returns 4, which is the index of the first capital letter "J"
diff --git a/files/zh-cn/web/javascript/reference/global_objects/string/startswith/index.html b/files/zh-cn/web/javascript/reference/global_objects/string/startswith/index.html
index 7cbd48ba7b..ac60af390f 100644
--- a/files/zh-cn/web/javascript/reference/global_objects/string/startswith/index.html
+++ b/files/zh-cn/web/javascript/reference/global_objects/string/startswith/index.html
@@ -21,7 +21,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/String/startsWith
 
 

语法

-
str.startsWith(searchString[, position])
+
str.startsWith(searchString[, position])

参数

@@ -44,7 +44,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/String/startsWith

此方法已被添加至 ECMAScript 2015 规范之中,但可能不能在所有的现行 JavaScript 实现中使用。不过,你可以用以下的代码段为 String.prototype.startsWith() 制作 Polyfill:

-
if (!String.prototype.startsWith) {
+
if (!String.prototype.startsWith) {
     Object.defineProperty(String.prototype, 'startsWith', {
         value: function(search, pos) {
             pos = !pos || pos < 0 ? 0 : +pos;
@@ -60,7 +60,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/String/startsWith
 
 

使用 startsWith()

-
var str = "To be, or not to be, that is the question.";
+
var str = "To be, or not to be, that is the question.";
 
 alert(str.startsWith("To be"));         // true
 alert(str.startsWith("not to be"));     // false
diff --git a/files/zh-cn/web/javascript/reference/global_objects/string/substring/index.html b/files/zh-cn/web/javascript/reference/global_objects/string/substring/index.html
index e2b061d1a3..f35badbe86 100644
--- a/files/zh-cn/web/javascript/reference/global_objects/string/substring/index.html
+++ b/files/zh-cn/web/javascript/reference/global_objects/string/substring/index.html
@@ -11,7 +11,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/String/substring
 
 

语法

-
str.substring(indexStart[, indexEnd])
+
str.substring(indexStart[, indexEnd])

参数

@@ -44,7 +44,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/String/substring

下例使用 substring 输出字符串 "Mozilla" 中的字符:

-
var anyString = "Mozilla";
+
var anyString = "Mozilla";
 
 // 输出 "Moz"
 console.log(anyString.substring(0,3));
@@ -73,7 +73,7 @@ console.log(anyString.substring(0,10));
 
 

下面一个例子运用了    String.length 属性去获取指定字符串的倒数元素。显然这个办法更容易记住,因为你不再像上面那个例子那样去记住起始位置和最终位置。

-
// Displays 'illa' the last 4 characters
+
// Displays 'illa' the last 4 characters
 var anyString = 'Mozilla';
 var anyString4 = anyString.substring(anyString.length - 4);
 console.log(anyString4);
@@ -87,7 +87,7 @@ console.log(anyString5);

下例替换了一个字符串中的子字符串。可以替换单个字符和子字符串。该例结尾调用的函数将 "Brave New World" 变成了 "Brave New Web"。

-
function replaceString(oldS, newS, fullS) {
+
function replaceString(oldS, newS, fullS) {
 // Replaces oldS with newS in the string fullS
   for (var i = 0; i < fullS.length; i++) {
     if (fullS.substring(i, i + oldS.length) == oldS) {
@@ -101,7 +101,7 @@ replaceString("World", "Web", "Brave New World");

需要注意的是,如果 oldSnewS 的子字符串将会导致死循环。例如,尝试把 "Web" 替换成 "OtherWorld"。一个更好的方法如下:

-
function replaceString(oldS, newS,fullS){
+
function replaceString(oldS, newS,fullS){
   return fullS.split(oldS).join(newS);
 }
diff --git a/files/zh-cn/web/javascript/reference/global_objects/string/tolowercase/index.html b/files/zh-cn/web/javascript/reference/global_objects/string/tolowercase/index.html index 007d9bb2d9..b1b9f6266d 100644 --- a/files/zh-cn/web/javascript/reference/global_objects/string/tolowercase/index.html +++ b/files/zh-cn/web/javascript/reference/global_objects/string/tolowercase/index.html @@ -15,7 +15,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/String/toLowerCase

语法

-
str.toLowerCase()
+
str.toLowerCase()
 

@@ -32,7 +32,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/String/toLowerCase

例子:使用 toLowerCase()

-
console.log('中文简体 zh-CN || zh-Hans'.toLowerCase());
+
console.log('中文简体 zh-CN || zh-Hans'.toLowerCase());
 // 中文简体 zh-cn || zh-hans
 
 ​console.log( "ALPHABET".toLowerCase() );
diff --git a/files/zh-cn/web/javascript/reference/global_objects/string/touppercase/index.html b/files/zh-cn/web/javascript/reference/global_objects/string/touppercase/index.html
index 0e45f82bac..820f1a80b3 100644
--- a/files/zh-cn/web/javascript/reference/global_objects/string/touppercase/index.html
+++ b/files/zh-cn/web/javascript/reference/global_objects/string/touppercase/index.html
@@ -19,7 +19,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/String/toUpperCase
 
 

语法

-
str.toUpperCase()
+
str.toUpperCase()

返回值

@@ -40,14 +40,14 @@ translation_of: Web/JavaScript/Reference/Global_Objects/String/toUpperCase

基本用法

-
console.log('alphabet'.toUpperCase()); // 'ALPHABET'
+
console.log('alphabet'.toUpperCase()); // 'ALPHABET'
 

将非字符串类型的 this (上下文)转为字符串

此方法会将任何非字符串类型的值转为字符串, 当你将其上下文 this 值设置为非字符串类型

-
const a = String.prototype.toUpperCase.call({
+
const a = String.prototype.toUpperCase.call({
   toString: function toString() {
     return 'abcdef';
   }
diff --git a/files/zh-cn/web/javascript/reference/global_objects/string/trim/index.html b/files/zh-cn/web/javascript/reference/global_objects/string/trim/index.html
index 503a9e234f..aa0e524f53 100644
--- a/files/zh-cn/web/javascript/reference/global_objects/string/trim/index.html
+++ b/files/zh-cn/web/javascript/reference/global_objects/string/trim/index.html
@@ -26,7 +26,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/String/Trim
 
 

语法

-
str.trim()
+
str.trim()

返回值

@@ -42,7 +42,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/String/Trim

下面的例子中将显示小写的字符串 'foo':

-
var orig = '   foo  ';
+
var orig = '   foo  ';
 console.log(orig.trim()); // 'foo'
 
 // 另一个 .trim() 例子,只从一边删除
@@ -55,7 +55,7 @@ console.log(orig.trim()); // 'foo'
 
 

如果 trim() 不存在,可以在所有代码前执行下面代码

-
if (!String.prototype.trim) {
+
if (!String.prototype.trim) {
   String.prototype.trim = function () {
     return this.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
   };
diff --git a/files/zh-cn/web/javascript/reference/global_objects/string/valueof/index.html b/files/zh-cn/web/javascript/reference/global_objects/string/valueof/index.html
index 6af5bd5d2d..2d8754a2cc 100644
--- a/files/zh-cn/web/javascript/reference/global_objects/string/valueof/index.html
+++ b/files/zh-cn/web/javascript/reference/global_objects/string/valueof/index.html
@@ -11,7 +11,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/String/valueOf
 
 

语法

-
str.valueOf()
+
str.valueOf()

返回结果

@@ -27,7 +27,7 @@ translation_of: Web/JavaScript/Reference/Global_Objects/String/valueOf

使用 valueOf()

-
var x = new String('Hello world');
+
var x = new String('Hello world');
 console.log(x.valueOf()); // Displays 'Hello world'
 
-- cgit v1.2.3-54-g00ecf