JavaScript/格式转换:修订间差异
来自康健生活
小 文字替换 -“{{Leftrightflex|”替换为“{{Double-columns|” 标签:ReplaceText 移动版编辑 移动版网页编辑 |
|||
| 第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"]" | ||
| 第17行: | 第17行: | ||
== 字符串转布尔 == | == 字符串转布尔 == | ||
=== Boolean === | === Boolean === | ||
{{ | {{Double-columns|<syntaxhighlight lang="javascript"> | ||
let foo = 'true'; | let foo = 'true'; | ||
Boolean(foo);</syntaxhighlight>|{{Consoleout|<pre> | Boolean(foo);</syntaxhighlight>|{{Consoleout|<pre> | ||
| 第24行: | 第24行: | ||
=== eval === | === eval === | ||
{{ | {{Double-columns|<syntaxhighlight lang="javascript"> | ||
let foo = 'true'; | let foo = 'true'; | ||
eval(foo.toLowerCase());</syntaxhighlight>|{{Consoleout|<pre> | eval(foo.toLowerCase());</syntaxhighlight>|{{Consoleout|<pre> | ||
| 第31行: | 第31行: | ||
=== <nowiki>===</nowiki> === | === <nowiki>===</nowiki> === | ||
{{ | {{Double-columns|<syntaxhighlight lang="javascript"> | ||
let foo = 'true'; | let foo = 'true'; | ||
foo === 'true' ? true : foo === 'false' ? false : foo;</syntaxhighlight>|{{Consoleout|<pre> | foo === 'true' ? true : foo === 'false' ? false : foo;</syntaxhighlight>|{{Consoleout|<pre> | ||
| 第38行: | 第38行: | ||
=== 正则 === | === 正则 === | ||
{{ | {{Double-columns|<syntaxhighlight lang="javascript"> | ||
let foo = 'true'; | let foo = 'true'; | ||
String.prototype.bool = function() { | String.prototype.bool = function() { | ||