vue替换空格
vue怎么替换空格?
vue:copy功能中替换字符串中的空格,换行符\r\n或\n去除
一:复制功能
export default {
methods: {
copy(value) {
try{
const textarea = document.createElement('textarea');
document.body.appendChild(textarea);//添加到body中
textarea.value = value;//给dom设置值
textarea.select();//设置选中
const copyFalse = document.execCommand('copy', false);
if(copyFalse){
this.alert('复制成功');
}else{
this.alert('复制失败');
}
textarea.remove();//用完移除dom
} 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);
本文出至:学新通
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通
- 本文地址: https://www.swvq.com/boutique/detail/1979
- 联系方式: luke.wu@swvq.com
- 来源链接: www.php.cn/vuejs/499326.html
系列文章
更多
同类精品
更多
精彩评论
-
docker hub 进不去怎么办
PHP中文网 03-15 -
2023年最新的28道PHP面试题附答案
PHP中文网 03-27 -
windows上查看nginx是否启动
PHP中文网 04-19 -
推荐五款xml编辑工具
PHP中文网 03-04 -
navicat怎样清除注册表
PHP中文网 04-05 -
ChatGPT应用通过Axios+EventSource使用GPT3.5 API
uWydnA 03-13 -
强力推荐10款好看使用的Bootstrap后台管理系统模板
PHP中文网 03-09 -
vscode怎么调整代码大小两种方法
PHP中文网 03-11 -
navicat连接sqlserver数据库
PHP中文网 04-03 -
navicat导入csv文件
PHP中文网 03-30