gpt4 book ai didi

javascript - 用字典查找和替换,只使用一次键

转载 作者:行者123 更新时间:2023-11-30 09:01:47 26 4
gpt4 key购买 nike

这是 Find multiple keywords within a dictionary 的后续.

我的问题是...

  1. 首先是:我认为这匹配的是不完整的单词。就像我的字典里有 short 一样,它很快就会匹配这个词。我该如何阻止呢?

  2. 第二个不是那么重要但会很好的是:我如何制作它以便每个内容只匹配一次?所以 short 不会在同一内容区域中被定义两次。

谢谢!

最佳答案

我已经实现了以下附加要求:

  • 在查找short时不要匹配shortly(因为shortly是不同的词)
  • 字典中的键只使用一次。
    输入示例:key=foo,replacement=bar,content=foo foo
    输出:bar foo(只替换第一个foo)。

演示:http://jsfiddle.net/bhGE3/3/

用法:

  1. 定义一个字典。每个 key 只能使用一次。
  2. 定义内容。将基于此字符串创建一个新字符串。
  3. 可选,定义一个replacehandler 函数。这个函数在每场比赛中被调用。返回值将用于替换匹配的短语。
    默认的replacehandler 将返回字典的匹配短语。该函数应采用两个参数:keydictionary
  4. 调用replaceOnceUsingDictionary(dictionary, content, replacehandler)
  5. 处理输出,例如。向用户显示内容

代码:

var dictionary = {
"history": "war . ",
"no": "in a",
"nothing": "",
"oops": "",
"time": "while",
"there": "We",
"upon": "in",
"was": "get involved"
};
var content = "Once upon a time... There was no history. Nothing. Oops";
content = replaceOnceUsingDictionary(dictionary, content, function(key, dictionary){
return '_' + dictionary[key] + '_';
});
alert(content);
// End of implementation

/*
* @name replaceOnceUsingDictionary
* @author Rob W http://stackoverflow.com/users/938089/rob-w
* @description Replaces phrases in a string, based on keys in a given dictionary.
* Each key is used only once, and the replacements are case-insensitive
* @param Object dictionary {key: phrase, ...}
* @param String content
* @param Function replacehandler
* @returns Modified string
*/
function replaceOnceUsingDictionary(dictionary, content, replacehandler) {
if (typeof replacehandler != "function") {
// Default replacehandler function.
replacehandler = function(key, dictionary){
return dictionary[key];
}
}

var patterns = [], // \b is used to mark boundaries "foo" doesn't match food
patternHash = {},
oldkey, key, index = 0,
output = [];
for (key in dictionary) {
// Case-insensitivity:
key = (oldkey = key).toLowerCase();
dictionary[key] = dictionary[oldkey];

// Sanitize the key, and push it in the list
patterns.push('\\b(?:' + key.replace(/([[^$.|?*+(){}])/g, '\\$1') + ')\\b');

// Add entry to hash variable, for an optimized backtracking at the next loop
patternHash[key] = index++;
}
var pattern = new RegExp(patterns.join('|'), 'gi'),
lastIndex = 0;

// We should actually test using !== null, but for foolproofness,
// we also reject empty strings
while (key = pattern.exec(content)) {
// Case-insensitivity
key = key[0].toLowerCase();

// Add to output buffer
output.push(content.substring(lastIndex, pattern.lastIndex - key.length));
// The next line is the actual replacement method
output.push(replacehandler(key, dictionary));

// Update lastIndex variable
lastIndex = pattern.lastIndex;

// Don't match again by removing the matched word, create new pattern
patterns[patternHash[key]] = '^';
pattern = new RegExp(patterns.join('|'), 'gi');

// IMPORTANT: Update lastIndex property. Otherwise, enjoy an infinite loop
pattern.lastIndex = lastIndex;
}
output.push(content.substring(lastIndex, content.length));
return output.join('');
}

关于javascript - 用字典查找和替换,只使用一次键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8672709/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com