替换单词Jquery,只有一次

时间:2019-08-07 10:08:47

标签: javascript jquery

我有这个JavaScript,但每次我在网站上保存该语言后, 在最后一个“ 0”示例“ italiano”上,如果我一次保存了更多个人资料,脚本添加了另一个“ 0”,例如“ Italianoo”,如果再次保存了“ Italianooo”

您如何解决该问题?也没有字典

非常感谢

var dictionary = {
  "Afrikanns": "Africano",
  "Albanian": "Albanese",
  "Arabic": "Arabo",
  "Armeno": "Armenian",
  "Basque": "Basco",
  "Bengali": "Bengalese",
  "Bielorussian": "Bielorusso",
  "Bulgarian": "Bulgaro",
  "Catalan": "Catalano",
  "Cambodian": "Cambogiano",
  "Czech": "Ceco",
  "Cinese": "Cinese",
  "Korean": "Coreano",
  "Croatian": "Croato",
  "Danish": "Danese",
  "Hebrew": "Ebraico",
  "Estonian": "Estone",
  "Fijan": "Fiji",
  "Finnish": "Finlandese",
  "French": "Francese",
  "Georgian": "Georgiano",
  "Japanese": "Giapponese",
  "Javanese": "Giavanese",
  "Greek": "Greco",
  "Gujarati": "Gujarati",
  "Hindi": "Indiano",
  "English": "Inglese",
  "Indonesian": "Indonesiano",
  "Irish": "Irlandese",
  "Icelandic": "Islandese",
  "Italian": "Italiano",
  "Latin": "Latino",
  "Latvian": "Lettone",
  "Lithuanian": "Lituano",
  "Macedonian": "Macedone",
  "Malayalam": "Malayalam",
  "Maltese": "Malese",
  "Maori": "Maori",
  "Marathi": "Marathi",
  "Mongolian": "Mongolo",
  "Nepali": "Nepalese",
  "Norwegian": "Norvegese",
  "Dutch": "Olandese",
  "Persian": "Persiano",
  "Polish": "Polacco",
  "Portoguese": "Portoghese",
  "Punjabi": "Punjabi",
  "Quechua": "Quechua",
  "Romanian": "Rumeno",
  "Russian": "Russo",
  "Samoan": "Samoan",
  "Serbian": "Serbo",
  "Slovak": "Slovacco",
  "Slovenian": "Sloveno",
  "Spanish": "Spagnolo",
  "Swedish": "Svedese",
  "Swahili": "Swahili",
  "Thai": "Tailandese",
  "Tamil": "Tamil",
  "Tatar": "Tatar",
  "German": "Tedesco",
  "Telugu": "Telugu",
  "Tibetan": "Tibetano",
  "Tonga": "Tonga",
  "Turkish": "Turco",
  "Ukranian": "Ucraino",
  "Hungarian": "Ungherese",
  "Urdu": "Urdu",
  "Uzbek": "Uzbeko",
  "Vietnamese": "Vietnamita",
  "Welsh": "Welsh",
  "Xhosa": "Xhosa"
};

jQuery(".upme-field-value > span").each(function() {
  for (var ptrn in dictionary) {
    jQuery(this).text(jQuery(this).text().replace(new RegExp(ptrn, "g"), dictionary[ptrn]));
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="upme-field-value">
  <span>Italian</span>
</div>

1 个答案:

答案 0 :(得分:0)

您正在搜索单词边界 \b

console.log("Italian".replace(/\bItalian\b/g, 'Italiano'))

console.log("Italiano".replace(/\bItalian\b/g, 'Italiano'))