JavaScript/格式转换:修订间差异
来自康健生活
小 文字替换 -“{{Leftrightflex|”替换为“{{Double-columns|” 标签:ReplaceText 移动版编辑 移动版网页编辑 |
|||
| (未显示同一用户的1个中间版本) | |||
| 第1行: | 第1行: | ||
== 引言 == | == 引言 == | ||
== 字符串转数组 == | == 字符串转数组 == | ||
{{ | {{Double-columns|<syntaxhighlight lang="javascript"> | ||
"123456".replace(/(.)(?=[^$])/g, "$1,").split(",");</syntaxhighlight>|{{Consoleout|<pre> | "123456".replace(/(.)(?=[^$])/g, "$1,").split(",");</syntaxhighlight>|{{Consoleout|<pre> | ||
(6) ["1", "2", "3", "4", "5", "6"] | (6) ["1", "2", "3", "4", "5", "6"] | ||
</pre>}}}} | </pre>}}}} | ||
== 数组转字符串 == | == 数组转字符串 == | ||
{{ | {{Double-columns|<syntaxhighlight lang="javascript"> | ||
["1", "2", "3", "4", "5", "6"].join('');</syntaxhighlight>|{{Consoleout|<pre> | ["1", "2", "3", "4", "5", "6"].join('');</syntaxhighlight>|{{Consoleout|<pre> | ||
"123456" | "123456" | ||
</pre>}}}} | </pre>}}}} | ||
== 数组字符串 == | == 数组字符串 == | ||
{{ | {{Double-columns|<syntaxhighlight lang="javascript"> | ||
JSON.stringify("123456".replace(/(.)(?=[^$])/g, "$1,").split(","));</syntaxhighlight>|{{Consoleout|<pre> | JSON.stringify("123456".replace(/(.)(?=[^$])/g, "$1,").split(","));</syntaxhighlight>|{{Consoleout|<pre> | ||
"["1","2","3","4","5","6"]" | "["1","2","3","4","5","6"]" | ||
</pre>}}}} | </pre>}}}} | ||
== 字符串转布尔 == | == 字符串转布尔 == | ||
<syntaxhighlight lang="javascript"> | === Boolean === | ||
Boolean( | {{Double-columns|<syntaxhighlight lang="javascript"> | ||
let foo = 'true'; | |||
Boolean(foo);</syntaxhighlight>|{{Consoleout|<pre> | |||
true | |||
</pre>}}}} | |||
<syntaxhighlight lang="javascript"> | === eval === | ||
{{Double-columns|<syntaxhighlight lang="javascript"> | |||
let foo = 'true'; | |||
eval(foo.toLowerCase());</syntaxhighlight>|{{Consoleout|<pre> | |||
true | |||
</pre>}}}} | |||
=== <nowiki>===</nowiki> === | |||
{{Double-columns|<syntaxhighlight lang="javascript"> | |||
let foo = 'true'; | |||
foo === 'true' ? true : foo === 'false' ? false : foo;</syntaxhighlight>|{{Consoleout|<pre> | |||
true | |||
</pre>}}}} | |||
<syntaxhighlight lang="javascript"> | === 正则 === | ||
{{Double-columns|<syntaxhighlight lang="javascript"> | |||
let foo = 'true'; | |||
String.prototype.bool = function() { | |||
return (/^true$/i).test(this); | |||
}; | |||
foo.bool();</syntaxhighlight>|{{Consoleout|<pre> | |||
true | |||
</pre>}}}} | |||