一:复制功能
export default {
methods: {
copy(value) {
try{
const textarea = document.createElement('textarea');
document.body.appendChild(textarea);
textarea.value = value;
textarea.select();
const copyFalse = document.execCommand('copy', false);
if(copyFalse){
this.alert('复制成功');
}else{
this.alert('复制失败');
}
textarea.remove();
} catch {
this.alert('复制失败')
}
}
}
}
二:字符串中的空格,换行符\r\n或\n替换
为了让回车换行符正确显示,需要将 \n 或 \r\n 替换成 <br>。同样地,将空格替换存 。这里我们通过正则表达式来替换。
一、替换所有的空格、回车换行符
var string = "欢迎访问!\r\nchifanlema.com";
string = string.replace(/\r\n/g,"<br>")
string = string.replace(/\n/g,"<br>");
string = string.replace(/\s/g," ");
console.log(string);
二、去掉所有的空格、回车换行符
var string = "欢迎访问!\r\nchifanlema.com ";
string = string.replace(/\r\n/g,"")
string = string.replace(/\n/g,"");
string = string.replace(/\s/g,"");
console.log(string);