一份自己最近用到的一些小技巧,你知道吗?

  js替换字符串中的字符_js替换指定位置的字符_js字符串替换某个字符

  关注前端达人,与你共同进步

  js字符串替换某个字符_js替换字符串中的字符_js替换指定位置的字符

  开篇

  今天笔者整理了一份自己最近用到的一些小技巧分享给大家,虽然都是基础技术js替换字符串中的字符,不过在某些特殊时刻还蛮有用的,不至于加载一堆体积庞大的第三方库,今天笔者用本文归纳一下分享给大家。本篇文章主要包含以下内容:

  本篇文章阅读时间预计3分钟。

  01

  产生随机不重复ID

  有时候在没有第三方类库的情况下,我们希望希望产生随机且不重复的ID,这时我会使用「随机数」搭配「时间戳」的方式,首先使用Math.random()产生0~1之间约16~17位数的随机浮点数,再通过toString(36)的方法缩短其位数,并转换为36进位( 0~9、a~z ),若嫌太长还可用substr方法进行自行截取。

  <pre class="selected" style="margin-top: 15px;margin-bottom: 15px;padding: 10px 5px 10px 10px;border-width: 0px;border-style: initial;border-color: initial;font-style: inherit;font-variant: inherit;font-weight: inherit;font-stretch: inherit;font-size: 13px;line-height: 18px;font-family: monaco, Consolas, "Liberation Mono", Courier, monospace;vertical-align: baseline;box-sizing: border-box;overflow-wrap: normal;background: rgb(240, 240, 240);border-radius: 3px;overflow-x: auto;">Math.random().toString(36).substr(2,n); // 截取小数点后的部分,n在实际应用中修改成你需要的位数</pre>

  第二步使用Date.now()取得目前的毫秒数,总共有13位数,和上个方法一样我们通过toString(36)的方法缩短其位数,并转换为36进位,接着用substr自行截取需要的位数。

  <pre class="" style="margin-top: 15px;margin-bottom: 15px;padding: 10px 5px 10px 10px;border-width: 0px;border-style: initial;border-color: initial;font-style: inherit;font-variant: inherit;font-weight: inherit;font-stretch: inherit;font-size: 13px;line-height: 18px;font-family: monaco, Consolas, "Liberation Mono", Courier, monospace;vertical-align: baseline;box-sizing: border-box;overflow-wrap: normal;background: rgb(240, 240, 240);border-radius: 3px;overflow-x: auto;">Date.now().toString(36).substr(4,n); //由于前几位固定不变,我们从第5位开始进去截取获取随机数,n在实际应用中修改成你需要的位数</pre>

  将随机数的片段和时间戳的片段进行组合,就能产生一个随机不重复的id 。( 应该说重复机率低到不可能重复,如果你遇到重复的,恭喜你,建议你今天买彩票 )

  <pre class="selected" style="margin-top: 15px;margin-bottom: 15px;padding: 10px 5px 10px 10px;border-width: 0px;border-style: initial;border-color: initial;font-style: inherit;font-variant: inherit;font-weight: inherit;font-stretch: inherit;font-size: 13px;line-height: 18px;font-family: monaco, Consolas, "Liberation Mono", Courier, monospace;vertical-align: baseline;box-sizing: border-box;overflow-wrap: normal;background: rgb(240, 240, 240);border-radius: 3px;overflow-x: auto;">Math.random().toString(36).substr(2,n) + Date.now().toString(36).substr(4,n);</pre>

  02

  模板标签替换

  开发过程中有时会遇到要取出一段文字中的某些值,如果单纯只是要「替换成别的值」,使用replace()的方法就能轻松实现,对以下这段字符串来说,里面有几个利用{{}}包覆的标签需要你替换,你会怎么做呢:

  大家好,我的公众号是{{name}},今年{{age}}岁了,创建于{{year}}年,欢迎你的关注

  如果想要把所有「{{}}」两个大括号内的字替换成对应的值,可以通过以下的方式进行处理,核心的内容就是replace()方法里的正规表达式/{{(.*?)}}/g,/{|}/g:

  <pre class="selected" style="margin-top: 15px;margin-bottom: 15px;padding: 10px 5px 10px 10px;border-width: 0px;border-style: initial;border-color: initial;font-style: inherit;font-variant: inherit;font-weight: inherit;font-stretch: inherit;font-size: 13px;line-height: 18px;font-family: monaco, Consolas, "Liberation Mono", Courier, monospace;vertical-align: baseline;box-sizing: border-box;overflow-wrap: normal;background: rgb(240, 240, 240);border-radius: 3px;overflow-x: auto;">const text = '大家好,我的公众号是{{name}},今年{{age}}岁了,创建于{{year}}年,欢迎你的关注’; const obj = { name: '前端达人', age: 5, year: 2014 }; const result = text.replace(/{{(.*?)}}/g, e => { return obj[e.replace(/{|}/g,'')]; });</pre>

  得到的结果就会是:「大家好,我的公众号是前端达人,今年5岁了,创建于2014年,欢迎你的关注」。

  03

  String 转 XML、XML 转 String

  在JavaScript里如果直接读取XML,得到的会是一些XML节点构成的对象,如果要使用像是replace()..等字串的操作就必须转换成字串String才行,下方代码可以很简单的将XML转换成字串String,处理之后再转回XML。(注:ie需要特殊处理,感兴趣的可以去百度搜索)

  XML转字串String

  <pre class="selected" style="margin-top: 15px;margin-bottom: 15px;padding: 10px 5px 10px 10px;border-width: 0px;border-style: initial;border-color: initial;font-style: inherit;font-variant: inherit;font-weight: inherit;font-stretch: inherit;font-size: 13px;line-height: 18px;font-family: monaco, Consolas, "Liberation Mono", Courier, monospace;vertical-align: baseline;box-sizing: border-box;overflow-wrap: normal;background: rgb(240, 240, 240);border-radius: 3px;overflow-x: auto;">xmlToString = (new XMLSerializer()).serializeToString(xmlObject);</pre>

  字串String转XML

  <pre class="selected" style="margin-top: 15px;margin-bottom: 15px;padding: 10px 5px 10px 10px;border-width: 0px;border-style: initial;border-color: initial;font-style: inherit;font-variant: inherit;font-weight: inherit;font-stretch: inherit;font-size: 13px;line-height: 18px;font-family: monaco, Consolas, "Liberation Mono", Courier, monospace;vertical-align: baseline;box-sizing: border-box;overflow-wrap: normal;background: rgb(240, 240, 240);border-radius: 3px;overflow-x: auto;">stringToXML = (new DOMParser()).parseFromString(xmlString, "text/xml");</pre>

  04

  快速取整数

  在JavaScript中取整数js替换字符串中的字符,最常见的不外乎就是Math.round()四舍五入、Math.floor()返回小于等于给定数字的最大整数和 Math.ceil()函数返回大于或等于一个给定数字的最小整数(无条件进位)三种方法,不过如果通过两个「按位取反两次」~~(两个蚯蚓符号),也可以做到无条件舍去小数点的效果,这也是最快可以取整数的方法,下方的代码会先取得一个0~100的随机数,然后取出整数的部分。

  <pre class="selected" style="margin-top: 15px;margin-bottom: 15px;padding: 10px 5px 10px 10px;border-width: 0px;border-style: initial;border-color: initial;font-style: inherit;font-variant: inherit;font-weight: inherit;font-stretch: inherit;font-size: 13px;line-height: 18px;font-family: monaco, Consolas, "Liberation Mono", Courier, monospace;vertical-align: baseline;box-sizing: border-box;overflow-wrap: normal;background: rgb(240, 240, 240);border-radius: 3px;overflow-x: auto;">const num = Math.random() * 100; console.log(num); // 输出原本的随机数 console.log(~~num); // 无条件舍去小数部分,类似Math.floor()</pre>

  ~是按位取反运算,是取反两次。在这里的作用是去掉小数部分,因为位运算的操作值要求是整数,其结果也是整数,所以经过位运算的都会自动变成整数。与Math.floor()不同的是,它只是单纯的去掉小数部分,不论正负都不会改变整数部分。

  js替换指定位置的字符_js字符串替换某个字符_js替换字符串中的字符

  荷花开了——笔者上周末在后海拍摄

  今天JS小技巧的分享就到这里,希望能在工作中对你有所帮助,建议大家经常整理梳理自己日常工作中会用到的小方法和小技巧,维护到自己的方法库里,到时用的时候会事半功倍。

  在接下来的文章里,笔者会持续关注这方面的内容,将会整理成文章分享到这个系列里,欢迎你持续关注与订阅。

  如果你喜欢本篇文章,请收藏本文,再给本文点个在看。

文章由官网发布,如若转载,请注明出处:https://www.veimoz.com/1516
0 评论
415

发表评论

!