本文介绍了用Javascript中设置的字典数组替换字符串中的字母的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为

"Pizza2Day!"

我要使用数组集替换的

.在这个数组集中,我将原始字母作为参考

that I want to replace using a array set. In this array set I have the original alphabets as reference

var originalValues = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'];

然后我设置了加密密钥

var encryptedValues = ['m', 'h', 't', 'f', 'g', 'k', 'b', 'p', 'j', 'w', 'e', 'r', 'q', 's', 'l', 'n', 'i', 'u', 'o', 'x', 'z', 'y', 'v', 'd', 'c', 'a'];

我想做的是在普通字符串(a,b,c,d)的原始字符串"Pizza2Day"中找到所有出现的内容,并将它们替换为加密集中(m,n,h,f)中的替换项.因此结果将是"Njaam2Fmc!"

what I want to do is find any occurrences in the original string "Pizza2Day" of normal alphabets (a,b,c,d) and replace them with their substitutes in the encryption set (m,n,h,f). So the result would be "Njaam2Fmc!"

我将如何去做?在这种情况下,循环会派上用场吗?

How would I go about doing this? Will a loop come in handy in this case?

推荐答案

另一种方法.

var dictionary = { "a": "m", "b": "h", "c": "t", "d": "f", "e": "g", "f": "k", "g": "b", "h": "p", "i": "j", "j": "w", "k": "e", "l": "r", "m": "q", "n": "s", "o": "l", "p": "n", "q": "i", "r": "u", "s": "o", "t": "x", "u": "z", "v": "y", "w": "v", "x": "d", "y": "c", "z": "a", "A": "M", "B": "H", "C": "T", "D": "F", "E": "G", "F": "K", "G": "B", "H": "P", "I": "J", "J": "W", "K": "E", "L": "R", "M": "Q", "N": "S", "O": "L", "P": "N", "Q": "I", "R": "U", "S": "O", "T": "X", "U": "Z", "V": "Y", "W": "V", "X": "D", "Y": "C", "Z": "A" },
    string = "Pizza2Day!",
    result = string.replace(/[a-z]/gi, m => dictionary[m]);
    
    console.log(result);

这篇关于用Javascript中设置的字典数组替换字符串中的字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 04:10