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>
添加回答
舉報