https://jsfiddle.net/jt8mLhk0/1/
我尝试用其他东西将包装成括号的东西用字符串替换。我得到了非常奇怪的结果。
HTML / JS
var string = document.getElementById('test').innerHTML;
var replace = string.replace(/[prefix]/g, "replaced");
console.log(replace); // Look in developer tools console

<div id="test">A little [prefix] test</div>
&#13;
原始字符串
一点[前缀]测试
预期(我希望的)
一点点替换测试
结果
lreplacedttlreplaced [replacementreplacedreplacedreplacedreplacedreplacedreplaced] treplacedst
我还想替换多个匹配项,这就是我使用/g
的原因。
答案 0 :(得分:2)
你必须逃避CREATE OR REPLACE FUNCTION state_group_count_and_sum( state map<timestamp, frozen<tuple<bigint,double>>>, timestamp timestamp, value double )
CALLED ON NULL INPUT
RETURNS map<timestamp, frozen<tuple<bigint,double>>>
LANGUAGE java AS '
Date date = (Date) timestamp;
Calendar cal = Calendar.getInstance(); // locale-specific
cal.setTime(date);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
date = cal.getTime();
TupleValue tupleValue = state.get(date);
Long count = (Long) tupleValue.getLong(0);
if (count == null) count = 1L;
else count = count + 1L;
Double sum = (Double) tupleValue.getDouble(1);
if (sum == null) sum = value;
else sum = sum + value;
//if (tupleValue == null) ?
tupleValue.setLong(0, count);
tupleValue.setDouble(1, sum);
state.put(date, tupleValue);
return state; ' ;
CREATE OR REPLACE AGGREGATE group_count_and_sum(timestamp, double)
SFUNC state_group_count_and_sum
STYPE map<timestamp, frozen<tuple<bigint,double>>>
INITCOND {};
。
[
&#13;
var string = document.getElementById('test').innerHTML;
var replace = string.replace(/\[prefix]/g, "Replaced");
console.log(replace); // Look in developer tools console
&#13;
答案 1 :(得分:2)
你应该使用这个
string.replace(/\[prefix]/g, "Replaced");
答案 2 :(得分:2)
你需要以正则表达式模式转义__getitem__
。
方括号在正则表达式中起重要作用。查看How can I put \[\] (square brackets) in RegExp javascript?
[]
&#13;
var string = document.getElementById('test').innerHTML;
var replace = string.replace(/\[prefix\]/g, "replaced");
console.log(replace); // Look in developer tools console
&#13;