![](https://img-blog.csdnimg.cn/20201014180756724.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
实用代码片段
极品三三
这个作者很懒,什么都没留下…
展开
-
读取字符串字符长度
在JavaScript中,使用字符串的length属性可以读取字符串的长度。长度以字符为单位,该属性为只读属性。下面代码使用字符串的length属性获取字符串的长度。var s = "String 类型长度"; //定义字符串 console.log(s.length); //返回10个字符JavaScript支持的字符包括单字节、双字节两种类型,为了精确计算字符串的字节长度,可以采用下面方法来计算。扩展阅读为 String扩展原型方法byteLength(),该方法将枚...原创 2020-12-23 11:00:10 · 1138 阅读 · 0 评论 -
常用正则表达式
/^([1-9][0-9]*)$/ 大于0的正整数/^(0|[1-9][0-9]*)$/ 大于等于的0 的正整数/^\d+(?=\.{0,1}\d+$|$)/ 正整数 正小数 0/^(\d|[1-9]\d+)(\.\d+)?$//^\d+(?=\.{0,1}\d+$|$)/ 正整数 正小数/^(\d|[1-9]\d+)(\.\d{1,2})?$/正整数 正小数 两位小数...原创 2020-08-21 11:15:07 · 345 阅读 · 1 评论 -
div里面的图片垂直居中的方法
body结构<body> <div> <img src="1.jpg" alt="haha"> </div></body>现在主流是移动端,那方法五最常用,因此我将它前置方法:弹性布局flex<style type="text/css"> *{m转载 2019-03-05 11:02:33 · 3817 阅读 · 0 评论 -
常用的一些正则方法
验证手机号码 function isPoneAvailable(str) { var myreg=/^[1][3,4,5,7,8][0-9]{9}$/; if (!myreg.test(str)) { return false; } else { return true; }...原创 2019-03-01 13:56:34 · 110 阅读 · 0 评论 -
微信公众号 vue单页面开发 分享问题
路径必须http://test.yw11.com/# 后面带#/不能是http://test.yw11.com 否则分享就会直接调往首页原创 2018-08-24 09:23:00 · 1098 阅读 · 0 评论 -
超实用的 JavaScript 浏览器代码片段
Bottom visible (页面的底部是否可见)使用 scrollY,scrollHeight 和 clientHeight 来确定页面的底部是否可见。const bottomVisible = _ => document.documentElement.clientHeight + window.scrollY >= document.documentElement.scrol...转载 2018-02-09 14:45:15 · 253 阅读 · 0 评论 -
超实用的 JavaScript 日期代码片段
Get days difference between dates (获取两个日期之间相差的天数)计算 Date 对象之间的差异(以天为单位)const getDaysDiffBetweenDates = (dateInitial, dateFinal) => (dateFinal - dateInitial) / (1000 * 3600 * 24);// getDaysDiffBetwe...转载 2018-02-09 14:47:17 · 132 阅读 · 0 评论 -
超实用的 JavaScript 函数代码片段
Chain asynchronous functions (链式调用异步函数)循环遍历包含异步事件的函数数组,每次异步事件完成后调用 next 。const chainAsync = fns => { let curr = 0; const next = () => fns[curr++](next); next(); };/*chainAsync([ next => { co...转载 2018-02-09 14:52:32 · 147 阅读 · 0 评论 -
超实用的 JavaScript 数学方法(算法)代码片段
Collatz algorithm(考拉兹算法)如果 n 是偶数,则返回 n/2 。否则返回 3n+1 。const collatz = n => (n % 2 == 0) ? (n / 2) : (3 * n + 1);// collatz(8) --> 4// collatz(5) --> 16考拉兹猜想(英语:Collatz conjecture),又称为奇偶归一猜想、3n...转载 2018-02-09 14:58:10 · 250 阅读 · 0 评论 -
超实用的 JavaScript 其他(实验中)代码片段
Speech synthesis (语音合成,实验阶段)使用 SpeechSynthesisUtterance.voice 和 indow.speechSynthesis.getVoices() 将消息转换为语音。使用 window.speechSynthesis.speak() 播放消息。了解有关Web Speech API的SpeechSynthesisUtterance接口的更多信息。con...转载 2018-02-09 14:59:46 · 268 阅读 · 0 评论 -
超实用的 JavaScript 字符串代码片段
Anagrams of string (with duplicates) (字符串的排列组合,带有重复项)使用递归。 对于给定字符串中的每个字母,为其余字母创建所有部分字母。 使用 Array.map() 将字母与每个部分字母组合在一起,然后使用 Array.reduce() 将所有字母组合到一个数组中。 基本情况是字符串 length 等于 2 或 1 。const anagrams = str...原创 2018-02-09 15:11:09 · 271 阅读 · 0 评论 -
超实用的 JavaScript Utility 实用函数代码片段
Escape regular expression (转义正则表达式)使用 replace() 来转义特殊字符。JavaScript 代码:const escapeRegExp = str => str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');// escapeRegExp('(test)') -> \\(test\\)Get native...转载 2018-02-09 15:13:38 · 1116 阅读 · 0 评论 -
超实用的 JavaScript 数组代码片段
Array concatenation (数组拼接)使用 Array.concat() ,通过在 args 中附加任何数组 和/或 值来拼接一个数组。const ArrayConcat = [].concat(arr, ...args); // [1,"ew"].concat([3],[5])Array difference (数组比较)根据数组 b 创建一个 Set 对象,然后在数组 a 上使用...转载 2018-02-09 10:25:14 · 256 阅读 · 0 评论