我觉得这很愚蠢,因为我敢打赌,答案是盯着我,但这里就是这样。为什么在测试时不会替换我的正则表达式对象?
我从CSS样式textDecoration中取出一个字符串,并试图删除字符串的下划线部分(以及其周围的任何空格)。当我运行test()时它返回true,但是当我执行replace方法时,字符串没有改变。帮帮我?
我的代码:
textDecoration = function(str) {
var n_str = str + '|/\s' + str + '|/\s' + str + '/\s|' + str + '/\s';
var nre = new RegExp(n_str, "g");
debug_log('Found or not: ' + nre.test(txt));
txt.replace(nre, '');
debug_log('Result: ' + txt);
debug_log('-----------------------');
}
var txt = "underline";
debug_log('-----------------------');
debug_log('Starting String: ' + txt);
textDecoration("underline");
txt = "underline overline line-through";
debug_log('-----------------------');
debug_log('Starting String: ' + txt);
textDecoration("underline");
txt = "overline underline line-through";
debug_log('-----------------------');
debug_log('Starting String: ' + txt);
textDecoration("underline");
txt = "overline line-through underline";
debug_log('-----------------------');
debug_log('Starting String: ' + txt);
textDecoration("underline");
输出:
我很尴尬......谢谢。我会在8分钟内接受T-Minus。 – 2012-03-29 15:10:46