亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何從javascript中的單詞中間刪除雙引號

如何從javascript中的單詞中間刪除雙引號

HUWWW 2021-08-26 14:30:22
我有一個字符串,它是在文本框中輸入的輸入字符 -你好“你好嗎。 現在我想要刪除中間引入的雙引號,以便我得到最終字符串 - 你好嗎以下是我到目前為止所嘗試的。   <script> function myFunction() {  var str = 'Hello" How are you';  var patt = /[a-zA-Z0-9-.{}@!#$%&()":,?=+_-~?\s]/g; // describes all the special characters which are allowed.  var result = str.match(patt);   document.getElementById("demo").innerHTML = result; }</script>請求這里的任何人幫助我。
查看完整描述

3 回答

?
繁星點點滴滴

TA貢獻1803條經驗 獲得超3個贊

您可以使用replace帶有正則表達式的輸入字符串的方法來刪除該字符串中的雙引號。


該g標志(全球)用于替換的所有出現"的的字符串中。沒有它,它將僅替換第一次出現的。


const str = 'Hello" How are" you',

  regex = /"/g; /** "g" flag is used, you remove it to only replace first occurrence **/


console.log(str.replace(regex, ''));

編輯 :


你說輸入字符串是從一個input字段中獲取的,這里有一個演示將更新的("如果找到則已刪除)值從字段打印到 a div:


const inp = document.getElementById('input'),

  outputDefault = document.getElementById('output-default'),

  output = document.getElementById('output'),

  regex = /"/g;


inp.addEventListener('input', () => {

  /** the text typed as it is without no replacing **/

  outputDefault.textContent = inp.value;


  /** the text with replacing **/

  output.textContent = inp.value.replace(regex, '')

});

<input type="text" id="input" />

<div>the value typed as it is : <span id="output-default"></span></div>

<div>the value gets updated while you type : <span id="output"></span></div>


查看完整回答
反對 回復 2021-08-26
?
慕少森

TA貢獻2019條經驗 獲得超9個贊

使用String.replace()函數:

var result = str.replace(/"/g,'');


查看完整回答
反對 回復 2021-08-26
  • 3 回答
  • 0 關注
  • 211 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號