Regex 谷歌表单多次搜索并用重复字符串替换列表
我已经在 对于类似的问题 我在用剧本Regex 谷歌表单多次搜索并用重复字符串替换列表,regex,string,google-apps-script,google-sheets,replace,Regex,String,Google Apps Script,Google Sheets,Replace,我已经在 对于类似的问题 我在用剧本 function preg_quote( str ) { // http://kevin.vanzonneveld.net // + original by: booeyOH // + improved by: Ates Goral (http://magnetiq.com) // + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) // +
function preg_quote( str ) {
// http://kevin.vanzonneveld.net
// + original by: booeyOH
// + improved by: Ates Goral (http://magnetiq.com)
// + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// + bugfixed by: Onno Marsman
// * example 1: preg_quote("$40");
// * returns 1: '\$40'
// * example 2: preg_quote("*RRRING* Hello?");
// * returns 2: '\*RRRING\* Hello\?'
// * example 3: preg_quote("\\.+*?[^]$(){}=!<>|:");
// * returns 3: '\\\.\+\*\?\[\^\]\$\(\)\{\}\=\!\<\>\|\:'
return (str+'').replace(/([\\\.\+\*\?\[\^\]\$\(\)\{\}\=\!\<\>\|\:])/g, "\\$1");
}
function ARRAYREPLACE(input,fromList,toList,caseSensitive){
/* default behavior it is not case sensitive */
if( caseSensitive == undefined ){
caseSensitive = false;
}
/* if the from list it is not a list, become a list */
if( typeof fromList != "object" ) {
fromList = [ fromList ];
}
/* if the to list it is not a list, become a list */
if( typeof toList != "object" ) {
toList = [ toList ];
}
/* force the input be a string */
var result = input.toString();
/* iterates using the max size */
var bigger = Math.max( fromList.length, toList.length) ;
/* defines the words separators */
var arrWordSeparator = [ ".", ",", ";", " " ];
/* interate into the lists */
for(var i = 0; i < bigger; i++ ) {
/* get the word that should be replaced */
var fromValue = fromList[ ( i % ( fromList.length ) ) ]
/* get the new word that should replace */
var toValue = toList[ ( i % ( toList.length ) ) ]
/* do not replace undefined */
if ( fromValue == undefined ) {
continue;
}
if ( toValue == undefined ) {
toValue = "";
}
/* apply case sensitive rule */
var caseRule = "g";
if( !caseSensitive ) {
/* make the regex case insensitive */
caseRule = "gi";
}
/* for each end word char, make the replacement and update the result */
for ( var j = 0; j < arrWordSeparator.length; j++ ) {
/* from value being the first word of the string */
result = result.replace( new RegExp( "^(" + preg_quote( fromValue + arrWordSeparator[ j ] ) + ")" , caseRule ), toValue + arrWordSeparator[ j ] );
/* from value being the last word of the string */
result = result.replace( new RegExp( "(" + preg_quote( arrWordSeparator[ j ] + fromValue ) + ")$" , caseRule ), arrWordSeparator[ j ] + toValue );
/* from value in the middle of the string between two word separators */
for ( var k = 0; k < arrWordSeparator.length; k++ ) {
result = result.replace(
new RegExp(
"(" + preg_quote( arrWordSeparator[ j ] + fromValue + arrWordSeparator[ k ] ) + ")" ,
caseRule
),
/* need to keep the same word separators */
arrWordSeparator[ j ] + toValue + arrWordSeparator[ k ]
);
}
}
/* from value it is the only thing in the string */
result = result.replace( new RegExp( "^(" + preg_quote( fromValue ) + ")$" , caseRule ), toValue );
}
/* return the new result */
return result;
}
函数预引用(str){
// http://kevin.vanzonneveld.net
//+原创者:booeyOH
//+改进:Goral(http://magnetiq.com)
//+改进者:凯文·范·佐内维尔德(http://kevin.vanzonneveld.net)
//+错误修复者:Onno Marsman
//*例1:预报价($40);
//*返回1:“\$40”
//*示例2:preg_quote(“*RRRING*Hello?”);
//*返回2:“\*rring\*Hello\?”
//*示例3:preg_quote(“\\.+*?[^]$(){}=!\:”;
//*返回3:“\\.\+\*\?\[\^\]\$\(\)\{\}=\!\\\\\\\\\:”
返回(str+'').replace(/([\\.\+\*\?\[\^\]\$\(\)\{\\\\\\=\!\\\\\\\\\\;\:])/g,“\\$1”);
}
函数ARRAYREPLACE(输入、fromList、toList、区分大小写){
/*默认行为它不区分大小写*/
if(区分大小写==未定义){
区分大小写=错误;
}
/*如果源列表不是列表,则变为列表*/
if(typeof fromList!=“对象”){
fromList=[fromList];
}
/*如果要列出的对象不是列表,请变成列表*/
if(toList的类型!=“对象”){
toList=[toList];
}
/*强制输入为字符串*/
var result=input.toString();
/*使用最大大小进行迭代*/
var biger=Math.max(fromList.length,toList.length);
/*定义单词分隔符*/
变量arrWordSeparator=[“,”,“,”;“,”];
/*加入名单*/
对于(变量i=0;i
输入示例如下:
"
详情
- 吊带领款式
- 无袖
- 可拆卸 腰带
- 聚集式分层裙板
- 隐形 后拉链
- 后领开口,纽扣闭合
- 无衬里的尺寸
- 模特身高170厘米,穿着一件8号的平装 尺码:
- 尺码8,长139cm,腰33cm, 胸围-43厘米
- 10号长度-141厘米,腰围-35厘米,胸围- 45厘米,12号,长143厘米,腰37厘米,胸围47厘米
- 尺码14长-145厘米,腰围-38厘米,胸围-48厘米
- 模特统计:胸围-76厘米,腰围-57厘米,臀部-91厘米 服装护理材料-人造丝
- 单独冷手洗
- 吊带领款式
- 无袖
- 可拆卸 腰带
- 聚集式分层裙板
- 隐形 后拉链
- 后领开口,纽扣闭合
- 无衬里的尺寸
- 模特身高5英尺6英寸,穿着8号的平底鞋 尺寸:8号,长54.5英寸,腰13英寸, 胸围-43厘米
- 10号长度-55.5英寸,腰围-14英寸,胸围- 45厘米
- 尺码12长-56.5英寸,腰围-14.5英寸,胸围- 47厘米
- 尺码14长57英寸,腰围15英寸,胸围48厘米
- 型号统计:胸围-30英寸,腰围-22.5英寸,臀部-91厘米 服装护理
- 材料-人造丝
- 单独冷手洗
每行上尺寸的第三个实例将被忽略,整个尺寸也是如此。有人能告诉我脚本是否限制了一定数量的替换,或者我应该做什么更改来替换大小吗?提前感谢。测试代码后,问题似乎出在行尾的值上。如果将输入定义为多行JavaScript字符串,请确保在这些值后面留有空格
var输入=”\
细节\
\
吊带领式\
无袖\
可拆卸式腰带\
叠层裙板\
隐形拉链\
后领开口,纽扣闭合\
无衬里\
尺寸\
\
模特身高170厘米,穿8号的衣服\
衣片尺寸:\
尺码8长度-139厘米,腰围-33厘米,胸围-43厘米\//不
/* defines the words separators */
var arrWordSeparator = [ ".", ",", ";", " ", "\n", "\r" ];