JavaScript/格式转换:修订间差异

来自康健生活
Kangkk​(留言 | 贡献)
Kangkk​(留言 | 贡献)
小 文字替换 -“{{Leftrightflex|”替换为“{{Double-columns|”
标签:ReplaceText 移动版编辑 移动版网页编辑
 
(未显示同一用户的1个中间版本)
第1行: 第1行:
== 引言 ==
== 引言 ==
== 字符串转数组 ==
== 字符串转数组 ==
{{Leftrightflex|<syntaxhighlight lang="javascript">
{{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>}}}}
== 数组转字符串 ==
== 数组转字符串 ==
{{Leftrightflex|<syntaxhighlight lang="javascript">
{{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>}}}}
== 数组字符串 ==
== 数组字符串 ==
{{Leftrightflex|<syntaxhighlight lang="javascript">
{{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('ture')</syntaxhighlight>
{{Double-columns|<syntaxhighlight lang="javascript">
let foo = 'true';
Boolean(foo);</syntaxhighlight>|{{Consoleout|<pre>
true
</pre>}}}}


<syntaxhighlight lang="javascript">
=== eval ===
eval('true'.toLowerCase())</syntaxhighlight>
{{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">
=== 正则 ===
var hasAuth = '$!auth' === 'true' ;
{{Double-columns|<syntaxhighlight lang="javascript">
 
let foo = 'true';
1.$!auth 为 true 时
String.prototype.bool = function() {
 
    return (/^true$/i).test(this);  
var hasAuth = 'true' === 'true' ;
};  
console.log(hasAuth);//true
foo.bool();</syntaxhighlight>|{{Consoleout|<pre>
 
true
2.$!auth 为 false 时
</pre>}}}}
 
var hasAuth = 'false' === 'true' ;
console.log(hasAuth);//false</syntaxhighlight>