2008-03-12
Java中String.replace()时的正则表达式不会写了, :(
关键字: 请教
现在有这样的一个字符串:
String srcString = "Por Ti Baby (Featuring Flex)";
我想通过调用replace()方法把这里的空格和"( )"都替换为"_",
可忘了正则表达式怎么写了,只能很采取那种很笨的方法,连调三次replace()方法.
String srcString = "Por Ti Baby (Featuring Flex)";
我想通过调用replace()方法把这里的空格和"( )"都替换为"_",
可忘了正则表达式怎么写了,只能很采取那种很笨的方法,连调三次replace()方法.
评论
Hafeyang
2008-03-14
String.prototype.trim= function()
{
// 用正则表达式将前后空格
// 用空字符串替代。
return this.replace(/(^\s*)|(\s*$)/g, "");
}
String.prototype.isEmail = function(){
var tmpStr = this;
var email = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
return email.test(tmpStr)
}
String.prototype.replaceAll = function(pattern, text, ignoreCase)
{
if(RegExp.prototype.isPrototypeOf(pattern))
throw new Error("Not supported. Use \"g\" flag with regExp patterns.");
return this.replace(new RegExp(pattern, (ignoreCase === true ? "gi" : "g")), text);
}
//alert('aabbcc').replaceAll('a','');/returns bbcc;
{
// 用正则表达式将前后空格
// 用空字符串替代。
return this.replace(/(^\s*)|(\s*$)/g, "");
}
String.prototype.isEmail = function(){
var tmpStr = this;
var email = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
return email.test(tmpStr)
}
String.prototype.replaceAll = function(pattern, text, ignoreCase)
{
if(RegExp.prototype.isPrototypeOf(pattern))
throw new Error("Not supported. Use \"g\" flag with regExp patterns.");
return this.replace(new RegExp(pattern, (ignoreCase === true ? "gi" : "g")), text);
}
//alert('aabbcc').replaceAll('a','');/returns bbcc;
发表评论
提醒: 该博客已发表在公共论坛,博客所有留言会成为论坛回贴,留言请注意遵守论坛发贴规则
- 浏览: 29426 次
- 性别:

- 来自: 北京

- 详细资料
搜索本博客
链接
最新评论
-
对浏览器中的context menu ...
Drag dropper
-- by whqida -
Spring中的load-time weav ...
没啥,有些class不在spring的管理之中,通过 load-time wea ...
-- by ray_linn -
对浏览器中的context menu ...
别搞笑了, 这是浏览器的 Context Menu? 这只不过是响应了鼠标右击事 ...
-- by fcoffee -
IE tab在Firefox里开发时 ...
firebug只能用于ff, 并不能用于ff下的ietab, 原因显而易见.如果 ...
-- by fcoffee -
IE tab在Firefox里开发时 ...
还网告知 该如何操作呢 我还是要到ie下再调试啊
-- by jianfeng008cn






评论排行榜