为了账号安全,请及时绑定邮箱和手机立即绑定

如何从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 关注
  • 185 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信