如何从javascript中的单词中间删除双引号

我有一个字符串,它是在文本框中输入的输入字符 -你好“你好吗。 现在我想要删除中间引入的双引号,以便我得到最终字符串 - 你好吗


以下是我到目前为止所尝试的。


   <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>

请求这里的任何人帮助我。


HUWWW
浏览 186回答 3
3回答

繁星点点滴滴

您可以使用replace带有正则表达式的输入字符串的方法来删除该字符串中的双引号。该g标志(全球)用于替换的所有出现"的的字符串中。没有它,它将仅替换第一次出现的。const str = 'Hello" How are" you',&nbsp; 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'),&nbsp; outputDefault = document.getElementById('output-default'),&nbsp; output = document.getElementById('output'),&nbsp; regex = /"/g;inp.addEventListener('input', () => {&nbsp; /** the text typed as it is without no replacing **/&nbsp; outputDefault.textContent = inp.value;&nbsp; /** the text with replacing **/&nbsp; 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>

慕少森

使用String.replace()函数:var&nbsp;result&nbsp;=&nbsp;str.replace(/"/g,'');
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript