小程序wxs、js使用正则替换字符串 | 熊阿哥博客

小程序wxs、js使用正则替换字符串

微信   2025-02-24 08:15   58   0  

把 2020-8-3 18:18:23 中的" - "替换为"/"


js用法:

var str ='2020-8-3 18:18:23';//以下两种任选一种str.replace(new RegExp('-','g'),'/');
str.replace(/-/g,'/');

console.log(str);


wxs用法1:

var datewxs=function(str){    var newdate = str.replace(getRegExp('-','g'),'/');    return newdate;
}

module.exports = {
  datewxs: datewxs
}<!--wxml引用-->
<wxs module="datewxs" src="../../utils/datewxs.wxs"></wxs>
<view>{{datewxs.datewxs('2020-8-3 18:18:23')}}</view>


输出为

2020/8/3 18:18:23


wxs用法2:

//item.text = "进入点击右下角扫码"
//item.red = "进入点击右下角"
<!--wxml-->
<wxs module="fn">
    module.exports = {
        replace: function(text, red){
            return text.replace(red, "<span class='red'>$&</span>");
        }
    }
</wxs>
<!--调用replace方法-->
<view class="title2" wx:key="item">
<rich-text nodes="{{fn.replace(item.text, item.red)}}"></rich-text>
</view>




博客评论
还没有人评论,赶紧抢个沙发~
发表评论
说明:请文明发言,共建和谐网络,您的个人信息不会被公开显示。