首頁  >  文章  >  web前端  >  vue怎麼替換空格

vue怎麼替換空格

藏色散人
藏色散人原創
2022-12-22 09:40:534231瀏覽

vue實作替換空格的方法:1、建立一個vue範例檔案;2、透過正規表示式「string = string.replace(/\s/g," ");」取代所有的空格; 3.透過「console.log(string);」輸出替換後的字串即可。

vue怎麼替換空格

本教學操作環境:Windows10系統、Vue 3版、Dell G3電腦。

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 替換成2a87851f231f0546ed00aa1a4409038e。同樣地,將空格替換存  。這裡我們透過正規表示式來替換。

一、替換所有的空格、回車換行符 

//原始字符串
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);

推薦學習:《vue .js影片教學

以上是vue怎麼替換空格的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn