将sum(something)的出现次数更改为something_sum
Change occurrences of sum(something) to something_sum
诚然,我对RegEx和模式替换很糟糕,所以我想知道是否有人能帮我解决这个问题,因为我已经尝试了几个小时,而且正在拔头发。
示例:
sum(Sales) needs to be converted to Sales_sum
max(Sales) needs to be converted to Sales_max
min(Revenue) needs to be converted to Revenue_min
唯一可用的前缀词将是sum, min, max, avg, xcount
——不确定这是否会对解决方案产生影响。
希望这是足够的信息来表明我正在努力做什么。这可能通过RegEx实现吗?
提前谢谢。
有几种可能的方法,例如:
var str = "min(Revenue)";
var arr = str.match(/([^(]+)'(([^)]+)/);
var result = arr[2]+'_'+arr[1];
则CCD_ 2为CCD_。
下面是一个更复杂的例子,在你的评论后面,处理许多匹配并降低动词的大小写:
var str = "SUM(Sales) + MIN(Revenue)";
var result = str.replace(/'b([^()]+)'(([^()]+)')/g, function(_,a,b){
return b+'_'+a.toLowerCase()
});
结果:"Sales_sum + Revenue_min"
尝试使用:
var input = 'sum(Sales)',
matches = input.match(/^([^(]*)'(([^)]*)/),
output = matches[2] + '_' + matches[1];
console.log(output); // Sales_sum
还有:
var input = 'sum(Sales)',
output = input.replace(/^([^(]*)'(([^)]*)')/, '$2_$1');
您可以将replace
与令牌一起使用:
'sum(Sales)'.replace(/('w+)'(('w+)')/, '$2_$1')
使用白名单作为前缀单词列表:
output = input.replace(/'b(sum|min|max|avg|xcount)'((.*?)')/gi,function(_,a,b) {
return b.toLowerCase()+"_"+a;
});
添加了'b
,一个单词边界。这可以防止类似"haxcount(xorz)"的内容变成"haxorz_xcount"
相关文章:
- 这.SOMETHING 总是返回未定义的 - extjs
- 如何在JavaScript中创建类似于something.function()的函数
- localStorage["something"] vs localStorage.somethin
- 我的localStorage结果显示了[null,null,“something”]],而不是“”;什么”;
- Javascript:如何称呼这个.Something() from this.img.onload = SomeLoa
- 雄辩的Javascript:无法理解在sum函数中如何确定数字值
- 什么是JS的确切等价物:PHP中的something.toFixed()
- javascript中的$(“something”)是什么意思
- 正则表达式将document.all.something替换为document.getElementById(“some
- 将sum(something)的出现次数更改为something_sum
- 使index.html的默认状态不带#something
- 为什么要将Something分配给Something.prototype.constructor
- React props={this.refs.something} undefined
- clearInterval - something's amiss
- 带有sum的jQuery选择器
- Unable to receive something in PHP from XMLHttpRequest's
- 如何通过循环更改 this.something 的值
- jQuery通过按键获取sum总是落后一步
- JSON 数据的 JavaScript SUM 和 GROUP BY
- 从文档和子文档按“$author”(MongoDB)分组$sum