后端返回字符串,前端给特定字符串设置链接

//后端返回的字符串 
//例如: 点击首页【<gt type="/pages/index/index?PageCur=0">餐券"</gt>】文字
// 将文字取出来
getText(e){
		const str = e
		
		// 使用正则表达式提取【<gt>】标签内的内容
		const regex = /<gt[^>]*>(.*?)<\/gt>/g;
		const matches = str.match(regex);
		if(Array.isArray(matches)){
			// 提取匹配结果中的餐券文本
			const result = matches.map(match => {
			  const innerTextRegex = />(.*?)</;
			  const innerTextMatch = match.match(innerTextRegex);
			  if (innerTextMatch && innerTextMatch.length > 1) {
			    return innerTextMatch[1];
			  }else{
				  return e
			  }
			});
			return '【'+ result[0] + '】'
		}else{
			return e
		}
	},
	// 将链接取出来
	getLink(e){
		const str = e
		const regex = /<gt type="(.*?)">/;
		const match = regex.exec(str);
		if (match) {
		  const typeValue = match[1];
		  return typeValue
		} else {
			return false
		  console.log("未找到匹配的type值");
		}
		
		
	},
//    拼接字符串处理文字链接
getContent(){
//模拟后端返回

const str = "点击首页【<gt type="/pages/index/index?PageCur=0">餐券</gt>】,设置【<gt type="/pages/mine/set-info">支付密码</gt>】,设置完成之后,再次点击餐券,【输入密码】出现餐券码,进行使用。"
		
		// 使用正则表达式提取【】内的内容
		const regex = /【([^】]+)】/g;
		const matches = str.match(regex);
		
		// 将结果拆分成数组格式
		const result = [];
		let currentIndex = 0;
		
		matches.forEach(match => {
		  const index = str.indexOf(match, currentIndex);
		  
		  if (index > currentIndex) {
		    result.push(str.substring(currentIndex, index));
		  }
		  
		  result.push(match);
		  currentIndex = index + match.length;
		});
		
		if (currentIndex < str.length) {
		  result.push(str.substring(currentIndex));
		}
		console.log(result,'result');
		let sayText = []
		console.log('result',result);
		result.map(item=>{
			console.log(this.getLink(item),this.getText(item));
			sayText.push({
				link:this.getLink(item),
				text:this.getText(item)
			})
		})



}

  • 6
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值