替换字符串数组中的字符[重复]
Posted
技术标签:
【中文标题】替换字符串数组中的字符[重复]【英文标题】:Replace a character(s) in array of a string [duplicate] 【发布时间】:2019-01-14 20:06:09 【问题描述】:我想用其他字符替换用户输入的特殊字符。
目前,我有以下不起作用。任何帮助将不胜感激。
$(document).ready(function()
$("#text_box_id").change(function ()
/*var name = $(this).val();
var dname_without_space = $("#text_box_id").val().replace(/ /g, "");*/
var specialCharList = ["Á","É","Í","Ó","Ú","á","é","í","ó","ú","ñ","Ñ"];
var replaceChar = ["A","E","I","O","U","a","e","i","o","u","n","N"];
var inputUser = $("#text_box_id").val();
var splitInput = inputUser.split(" ");
console.log(splitInput);
for(var i = 0; i < inputUser.length; i++)
for(var x = 0; x < specialCharList.length; x++)
if(splitInput[i] == specialCharList[x])
splitInput[i] = replaceChar[x];
var modInputUser = splitInput.join(" ");
console.log(modInputUser);
/*var name_without_special_char = $("#text_box_id").val().replace(/[^a-zA-Z 0-9]+/g, "");
$(this).val(name_without_special_char);
console.log(name_without_special_char)*/
);
);
【问题讨论】:
【参考方案1】:您可以拆分字符串(就像您已经在做但没有空格一样),然后通过在特殊字符列表中找到index 对每个字符运行map。如果索引存在(返回值大于-1
),则可以使用该索引从替换字符列表中获取值。
然后它看起来像这样:
$(document).ready(function()
$("#text_box_id").change(function()
let specialCharList = ["Á", "É", "Í", "Ó", "Ú", "á", "é", "í", "ó", "ú", "ñ", "Ñ"];
let replaceChar = ["A", "E", "I", "O", "U", "a", "e", "i", "o", "u", "n", "N"];
let inputUser = $("#text_box_id").val();
let newString = inputUser.split('').map(i =>
let idx = specialCharList.indexOf(i)
return idx > -1 ? replaceChar[idx] : i
).join('')
$("#text_box_id").val(newString)
);
);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="text_box_id">
【讨论】:
【参考方案2】:你可以试试这个:
function cleanString(str)
str = str.toLowerCase().replace(" ", "_");
str = str.replace(/[äâàáã]/g, "a").replace(/[ëêèéẽ]/g, "e").replace(/[ïîìíĩ]/g, "i");
str = str.replace(/[öôòóõ]/g, "o").replace(/[üûùúũ]/g, "u");
str = str.replace(/ç/g, "c").replace(/[^a-z0-9]/g, "");
return str;
测试
cleanString("áéíóú") // returns "aeiou"
【讨论】:
谢谢。你的方法似乎比我的方法更有效【参考方案3】:只是不要用空格分隔
$(document).ready(function()
$("#text_box_id").change(function ()
/*var name = $(this).val();
var dname_without_space = $("#text_box_id").val().replace(/ /g, "");*/
var specialCharList = ["Á","É","Í","Ó","Ú","á","é","í","ó","ú","ñ","Ñ"];
var replaceChar = ["A","E","I","O","U","a","e","i","o","u","n","N"];
var inputUser = $("#text_box_id").val();
var splitInput = inputUser.split("");
console.log(splitInput);
for(var i = 0; i < inputUser.length; i++)
for(var x = 0; x < specialCharList.length; x++)
if(splitInput[i] == specialCharList[x])
splitInput[i] = replaceChar[x];
var modInputUser = splitInput.join("");
console.log(modInputUser);
/*var name_without_special_char = $("#text_box_id").val().replace(/[^a-zA-Z 0-9]+/g, "");
$(this).val(name_without_special_char);
console.log(name_without_special_char)*/
);
);
这意味着拆分每个字符:
var splitInput = inputUser.split("");
【讨论】:
@Asthematic:谢谢。不敢相信我没有意识到这一点以上是关于替换字符串数组中的字符[重复]的主要内容,如果未能解决你的问题,请参考以下文章