在 JavaScript 中使用正则表达式自动完成最后一个不完整的短语
Autocomplete the last incomplete phrase using regex in JavaScript
我有一个句子如下所示
term = "How much new sales in new"
假设我得到了一些建议,例如 New York, New Delhi, Papua New Guinea
并且我 select New York
.
choice = "New York"
现在我需要确保任何与 selection 匹配的最新子字符串都替换为 selection。
所以理想情况下我的字符串现在应该变成
term = "How much new sales in New York"
所以这就是我所做的
terms = term.split(/\s+/g)
choice_terms = choice.split(/\s+/g)
terms.pop() //remove the last substring since this is what the user typed last
check_terms = terms
// get the latest instance of first term of the selection
if(user_choice_terms.length > 1) {
if(check_terms.lastIndexOf(user_choice_first_term) !== -1) {
last_index = check_terms.lastIndexOf(user_choice_first_term)
check_terms.splice(-last_index) //remove anything after the matched index
check_terms.push(...user_choice_terms) //add the selected term
return check_terms
}
}
但这似乎不是一个可靠的解决方案,我宁愿使用 regex
。用户也可以这样输入
term = "How much new sales in new yo"
这里我保证得到New York
反对yo
的建议,应该换成New York
是否有任何 regex
解决方案来确保最新的子字符串匹配完全替换为 selection?
注:我用的是jquery ui autocomplete
您可以创建一个模式来匹配 choice
所有可能的前缀,所有空格替换为 \s+
模式以匹配 1 个或多个空格并在末尾添加 $
仅匹配字符串末尾的模式:
/N(?:e(?:w(?:\s+(?:Y(?:o(?:r(?:k)?)?)?)?)?)?)?$/i
它将匹配 N
、Ne
、New
等,在 New
和 York
之间具有任意数量的空格,并且仅在由于 $
.
导致的字符串结尾
参见JavaScript演示:
const make_prefix = (string) => {
let s = string.charAt(0);
for (let i=1; i<string.length; i++) {
s += "(?:" + string.charAt(i);
}
for (let i=1; i<string.length; i++) {
s += ")?";
}
return s;
}
const term = "How much new sales in new yo";
const choice = "New York";
const regex = new RegExp(make_prefix(choice).replace(/\s+/g, '\s+') + '$', 'i');
console.log(term.replace(regex, choice))
// => How much new sales in New York
我有一个句子如下所示
term = "How much new sales in new"
假设我得到了一些建议,例如 New York, New Delhi, Papua New Guinea
并且我 select New York
.
choice = "New York"
现在我需要确保任何与 selection 匹配的最新子字符串都替换为 selection。
所以理想情况下我的字符串现在应该变成
term = "How much new sales in New York"
所以这就是我所做的
terms = term.split(/\s+/g)
choice_terms = choice.split(/\s+/g)
terms.pop() //remove the last substring since this is what the user typed last
check_terms = terms
// get the latest instance of first term of the selection
if(user_choice_terms.length > 1) {
if(check_terms.lastIndexOf(user_choice_first_term) !== -1) {
last_index = check_terms.lastIndexOf(user_choice_first_term)
check_terms.splice(-last_index) //remove anything after the matched index
check_terms.push(...user_choice_terms) //add the selected term
return check_terms
}
}
但这似乎不是一个可靠的解决方案,我宁愿使用 regex
。用户也可以这样输入
term = "How much new sales in new yo"
这里我保证得到New York
反对yo
的建议,应该换成New York
是否有任何 regex
解决方案来确保最新的子字符串匹配完全替换为 selection?
注:我用的是jquery ui autocomplete
您可以创建一个模式来匹配 choice
所有可能的前缀,所有空格替换为 \s+
模式以匹配 1 个或多个空格并在末尾添加 $
仅匹配字符串末尾的模式:
/N(?:e(?:w(?:\s+(?:Y(?:o(?:r(?:k)?)?)?)?)?)?)?$/i
它将匹配 N
、Ne
、New
等,在 New
和 York
之间具有任意数量的空格,并且仅在由于 $
.
参见JavaScript演示:
const make_prefix = (string) => {
let s = string.charAt(0);
for (let i=1; i<string.length; i++) {
s += "(?:" + string.charAt(i);
}
for (let i=1; i<string.length; i++) {
s += ")?";
}
return s;
}
const term = "How much new sales in new yo";
const choice = "New York";
const regex = new RegExp(make_prefix(choice).replace(/\s+/g, '\s+') + '$', 'i');
console.log(term.replace(regex, choice))
// => How much new sales in New York