字符串替换器和json

时间:2015-06-14 00:26:21

标签: javascript json

我正在一个应用程序中工作,该应用程序基本上会用字符串中的其他字符串替换字符串。

例如: "熔核"取代"鸡肉" http://localhost/api/logins/ object_list = list(Utilisateur.objects.values_list("Login"))

为此,我将以login作为输入。用户将为此输入JSON对象。

例如: input = "I would like some chicken"; output = "I would like some nuggets"; textarea

以下是我正在使用的代码:



replacing_box_input = "{"a":"b","b":"c","c":"d","chicken":"nuggets"}";

input = "I would like some chicken abc";

output = "I would like some nuggets bcd";




代码没有取代它应该的东西。我需要帮助搞清楚。你们中的任何一个人都知道为什么它没有像它应该的那样替换字符串吗?

2 个答案:

答案 0 :(得分:2)

这是一个没有连接到用户界面的工作示例:

String.prototype.cap = function () {
  return this.charAt(0).toUpperCase() + this.slice(1);
};

function trans(value, jsonMap) {
  var mapObj = JSON.parse(jsonMap);

  var re = new RegExp(Object.keys(mapObj).sort(function(a, b) {
    return b.length - a.length;
  }).join("|"), "g");

  return value.replace(re, function (matched) {
    return mapObj[matched];
  });
}

console.log(trans('I would like some chicken abc', '{"a":"b","b":"c","c":"d","chicken":"nuggets"}'));

运行时的输出是:

I would like some nuggets bcd

JSFiddle:http://jsfiddle.net/q80k9z2w/

此处它与您的用户界面相连:http://jsfiddle.net/d1rsof1q/2/

答案 1 :(得分:1)

尝试使用String.prototype.split()Array.prototype.map().val(function(index, value))



var output = $("#output");

var replacing_box_input = {
  "a": "b",
  "b": "c",
  "c": "d",
  "chicken": "nuggets"
};

function trans() {
  var dic_editor = $("#dic-editor");
  var fn = function(re, j) {
    return dic_editor.val().split(re).map(function(val, index) {
      return val in replacing_box_input ? replacing_box_input[val] : val
    }).join(j)
  };
  output.val(fn(/\s/, " ")).val(fn("", ""));
}

body {
  background-color: #cccccc;
  color: #444444;
}
h1 {
  text-align: center;
  font-family: "courier";
}
#explanation {
  text-align: justify;
  font-family: "Times New Roman";
  font-size: 1em;
  text-indent: 3em;
}
code {
  font-family: "courier new";
  font-size: .8em;
  padding: 2px;
  padding-left: 4px;
  padding-right: 4px;
  background-color: #111111;
  color: #aaaaaa;
  border-radius: 2px;
}
#dic-editor {
  width: calc(100% - .86%);
  width: -moz-calc(100% - .86%);
  height: calc(100vh - 1.86em);
  height: -moz-calc(100vh - 1.86em);
  padding: .43%;
  margin: 0;
  margin-bottom: 1em;
  border: none;
  background-color: #111111;
  color: #aaaaaa;
  border-radius: 2px;
  font-size: 1em;
  outline: none;
  resize: none;
  overflow: auto;
}
#input {
  width: calc(43% - .86%);
  width: -moz-calc(43% - .86%);
  height: 40vh;
  float: left;
  padding: .43%;
  margin: 0;
  border: none;
  background-color: #111111;
  color: #aaaaaa;
  border-radius: 2px;
  font-size: 1em;
  outline: none;
  resize: none;
  overflow: auto;
}
#inputB {
  font-family: "courier";
  width: 14%;
  padding: 0;
  margin: 0;
  padding-top: 3px;
  padding-bottom: 3px;
  border: none;
  background-color: #1f1f1f;
  color: #aaaaaa;
  border-radius: 2px;
  font-size: .8em;
  resize: none;
  cursor: pointer;
  outline: none;
}
#inputB:hover {
  background-color: #aaaaaa;
  color: #1f1f1f;
}
#output {
  width: calc(43% - .86%);
  width: -moz-calc(43% - .86%);
  height: 40vh;
  float: right;
  padding: .43%;
  margin: 0;
  border: none;
  background-color: #111111;
  color: #aaaaaa;
  border-radius: 2px;
  font-size: 1em;
  outline: none;
  resize: none;
  overflow: auto;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<h1>Conlang: A Generator</h1>

<textarea id="dic-editor">I would like some chicken abc</textarea>

<div id="translator">
  <textarea id="input"></textarea>
  <input type="button" value="Translate" id="inputB" onclick="trans()">
  <textarea id="output" readonly></textarea>
</div>
&#13;
&#13;
&#13;