【uniapp 开发】字符串工具类 StringUtil

替换字符串中的所有 “***” 子串

var str='Is this all there is';

var subStr=new RegExp('is','ig');//创建正则表达式对象,不区分大小写,全局查找
var result=str.replace(subStr,"");//把'is'替换为空字符串
console.log(result); //th all there 

var subStr=new RegExp('is','i');//创建正则表达式对象,区分大小写
var result=str.replace(subStr,"");//把'is'替换为空字符串
console.log(result); //this all there is

分割字符串为数组

var address= '111;222;333;444'
let addr = address.split(';');

隐藏店铺名称部分文字,至多隐藏四个字,

//例如:“ 欧莱雅旗舰店” 变为  **雅**店

	formatShopName: function(name) {
		var str_array = name.split("");
		
		if(str_array.length < 3){
			
		}
		else if(str_array.length == 3){
			name = name.replace(str_array[1],"*");
		}
		else if(str_array.length > 3){
			var index = 0;
			for (var i = 1; i < str_array.length; i+=2) {
				if(i!=str_array.length-1 && i!=str_array.length-2 && index<4){
					name = name.replace(str_array[i],"*");
					index ++;
					console.log('name=='+name);
				}
			}
		}
		
		return name;
	},

posted @ 2019-08-08 15:22  浪里小白龙呼呼呼  阅读(9685)  评论(0编辑  收藏  举报