在最后一个单词 Javascript 之前附加字符串 [英] Append string in before the last word Javascript
问题描述
我的 input1 是完美清洁"多动作"清洁
我的 input2 是非常干净"多动作"
My input1 is "perfectly clean" "multi action" cleansing
My input2 is "perfectly clean" "multi action"
我需要在最后一个位置附加and"例如:如果 input1 则输出应该是完全干净"多动作"和清洁如果 input2完全干净"和多动作"
I need to append "and" in the last but one position eg: if input1 then output should be "perfectly clean" "multi action" and cleansing if input2 "perfectly clean" and "multi action"
输入是动态的,但我需要像输入一样带有双代码的输出你能帮我解决一个满足输入的解决方案
The input is dynamic but I need the output with the double codes as in the input can u help me with a solution which satisfies both the input
我试过这样
var text = g_eCatalogMeta.searchCorrected,
clength = text.split('"');
clength = clength.filter(function(n){ return n != "" && n!=" "});
if (clength.length >1) {
var aa =clength.length;
clength.insert(aa-1, "und");
text = clength.join;
}
但无法获得双重代码和最后一个字.请帮帮我.
But could not get the double codes and the last word. Please help me out.
推荐答案
最简单的方法是在最后引用的字符串或最后一个单词之前插入 and
的正则表达式.
The easiest way is a regular expression that inserts and
before the last quoted string or the last word.
str.replace(/("[^"]+"|\w+)$/, "and $1");
https://regex101.com/r/yK5tD9/1
正则表达式细分:
( = start of group 1
"[^"]+" = quote, some non-quotes, quote
| = OR
\w+ = a word
) = end of group 1
$ = end of string
$1
替换表示任何第 1 组匹配".
$1
in replacement means "whatever group 1 matches".
这篇关于在最后一个单词 Javascript 之前附加字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!