JS如何正确替换引号上的"&quot"

6 javascript jquery twitter-bootstrap-3 bootstrap-select

<input type="text" autocomplete="off" class="Autocomlete1" name="test">

$('.Autocomlete1').typeahead({
            ajax: {
                    url: './test.php?log=test',
                    triggerLength: 1
                  },
            updater: function(item) {
                    return item;
                },
            onSelect: function(item) {

                    return item;
                }
    });
Run Code Online (Sandbox Code Playgroud)

在自动input编译后我们得到nextvalue - Text &quot; TextTextText &quot;(数据库行有它的值)但需要输出Text " TextTextText "

对于更换&quot;"我希望做:

onSelect: function(item) {
  var text = item.text;
  var text = text.replace(/&quot;/g, '"');
  $('.Autocomlete1').val(text);
  return item;
}
Run Code Online (Sandbox Code Playgroud)

但这不起作用......

请告诉我如何更换&quot报价?

Leo*_*oki 21

如果不工作var text = text.replace(/&quot;/g, '\\"');检查其他线路因为它对我有用.


Shi*_*tal 7

" 在字符串中写 '\" 之前也需要一个转义字符

 var text = text.replace(/&quot;/g, '\\"');
Run Code Online (Sandbox Code Playgroud)

  • 这对我有用,除了这种格式: `.replace(/"/g, '\"')` (2认同)