我想知道是否有更好的方法来处理大量的jquery / javascript替换。这就是我的代码目前的样子,我将添加更多的单词/短语。我想知道我是否应该使用数组或对象中的单词进行某种循环?或者甚至可以将这些词保存在不同的文件中?
input = input.replace(/\band\b/g, "&")
//COMMON ABREVIATIONS
.replace(/\bwith\b/g, "w/")
.replace(/\bwithout\b/g, "w/o")
.replace(/\bpeople\b/g, "ppl")
.replace(/\bbecause\b/g, "b/c")
.replace(/\bestablished\b/g, "est.")
.replace(/\bstreet\b/gi, "St.")
.replace(/\bavenue\b/gi, "Ave.")
.replace(/\bparkway\b/gi, "Pkwy.")
.replace(/\blane\b/gi, "Ln.")
.replace(/\bboulevard\b/gi, "Blvd.")
.replace(/\bassociates\b/gi, "Assoc.")
.replace(/\bretweet\b/gi, "RT")
.replace(/\bapartment\b/gi, "Apt.")
.replace(/\bdirect message\b/gi, "DM")
.replace(/\bphoto credit\b/, "PC")
.replace(/\bphoto cred\b/, "PC");
答案 0 :(得分:0)
我找到了一些东西,但不幸的是,它并没有用两个词来表达短语。我打赌,RegEx出了问题。
var input = {
'band': "&",
'with': "w/",
'without': "w/o",
'people': "ppl",
'because': "b/c",
'established': "est.",
'street': "St.",
'avenue': "Ave.",
'parkway': "Pkwy.",
'lane': "Ln.",
'boulevard': "Blvd.",
'associates': "Assoc.",
'retweet': "RT",
'apartment': "Apt.",
'direct message': "DM",
'photo credit': "PC",
'photo cred': "PC"
};
console.log('band with'.replace(/\w+/gi, match => input[match]));