我有这样的查询
class Base {}
class ClassA extends Base { private a: number}
class ClassB extends Base { private b: number}
type Condition = ClassA extends ClassB ? true : false; // false now
我需要用加号替换所有空格和逗号,所以我这样做:
2258, Hampton Street, Columbia, SC, USA
问题是当有多个逗号或空格时,它不起作用(或者查询末尾或开始处有空格或逗号。因此,我需要解决以下情况:
query = query.replace(/,? /g, '+');
它们都应该是:
2258, Hampton Street, Columbia, SC, USA
2258, Hampton Street, Columbia, SC,, USA,
2258, Hampton Street, Columbia, SC, USA ,
,2258, Hampton Street, Columbia, SC, USA
, 2258, Hampton Street, Columbia, SC, USA
2258, Hampton Street, Columbia, SC, USA
,,2258, Hampton Street, Columbia, SC, USA
2258, Hampton Street,,,, Columbia, SC, USA
2258, Hampton Street Columbia, SC, USA,
2258, Hampton Street ,, Columbia, SC, USA
答案 0 :(得分:2)
您可以使用
var rx = /^[,\s]+|[,\s]+$|([,\s]+)/g;
s = s.replace(rx, function(_,$1) { return $1 ? '+' : ''; })
模式匹配
^[,\s]+
-字符串开头的1+个逗号或空格字符|
-或[,\s]+$
-字符串末尾有1个以上的逗号或空格字符|
-或([,\s]+)
-捕获到第1组的1个以上逗号或空格字符如果与组1匹配的替换项是+
,则替换项是一个空字符串(删除开头/结尾的空格/逗号)。
请参阅JS演示
var strs = ['2258, Hampton Street, Columbia, SC, USA', '2258, Hampton Street, Columbia, SC,, USA, ', '2258, Hampton Street, Columbia, SC, USA ,', ' ,2258, Hampton Street, Columbia, SC, USA', ', 2258, Hampton Street, Columbia, SC, USA', ' 2258, Hampton Street, Columbia, SC, USA', ',,2258, Hampton Street, Columbia, SC, USA', '2258, Hampton Street,,,, Columbia, SC, USA ', '2258, Hampton Street Columbia, SC, USA,', '2258, Hampton Street ,, Columbia, SC, USA'];
var rx = /^[,\s]+|[,\s]+$|([,\s]+)/g;
for (var s of strs) {
console.log(s.replace(rx, function(_,$1) {
return $1 ? '+' : '';
}))
}
答案 1 :(得分:1)
尝试一下
select s.customer_id, sum(s.item_value) as total_purchase_amount,
(select sum(p.amount)
from Payment p
where p.customer_id = s.customer_id
) as total_paid_amount
from Sales s
where s.customer_id = ? -- you can pass here customer_id
group by s.customer_id;
示例:
q.replace(/^[, ]+|[, ]+$/g, '').replace(/[, ]+/g, '+')
答案 2 :(得分:0)
请检查以下两个步骤的示例
var inputString = `2258, Hampton Street, Columbia, SC, USA
2258, Hampton Street, Columbia, SC,, USA,
2258, Hampton Street, Columbia, SC, USA ,
,2258, Hampton Street, Columbia, SC, USA
, 2258, Hampton Street, Columbia, SC, USA
2258, Hampton Street, Columbia, SC, USA
,,2258, Hampton Street, Columbia, SC, USA
2258, Hampton Street,,,, Columbia, SC, USA
2258, Hampton Street Columbia, SC, USA,
2258, Hampton Street ,, Columbia, SC, USA`;
var regex = /^[\s*,]+|[\s*,]$|(\s*,*)$|(,*\s*)$|[ ]{2}/gm;
// Clean string first remove space or , in start and end and remove duplicated inside statements
inputString = inputString.replace(regex, '');
// Replace any , or space one or more with +
console.log(inputString.replace(/[, ]+/gm, '+'));