繁体   English   中英

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

[英]How to remove double quotes from a middle of a word in 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>

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

您可以使用正则表达式对输入字符串使用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场,这里的打印更新(有一个演示"从田间到删除,如果发现)值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>

使用String.replace()函数:

var result = str.replace(/"/g,'');
const stripQuotes = val => val.replace(/"/g, "");

stripQuotes("your textfields value");

这是一个工作示例: https : //jsbin.com/suvamufabi/1/edit?js,console,output

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM