3 回答
![?](http://img1.sycdn.imooc.com/54584d6100015f5802200220-100-100.jpg)
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>
添加回答
举报