//后端返回的字符串
//例如: 点击首页【<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)
})
})
}
后端返回字符串,前端给特定字符串设置链接
最新推荐文章于 2024-05-18 21:57:13 发布