今天我们来研究一点正则表达式

emu 2003-01-22 08:30:41
加精
function String.prototype.trim1()
{
return this.replace(/(^\s*)|(\s*$)/g, "");
}

function String.prototype.trim2()
{
return this.replace(/^\s*([^\s].*[^\s])\s*$/,"$1");
}

猜猜哪个跑起来快一点?
...全文
46 33 打赏 收藏 转发到动态 举报
写回复
用AI写文章
33 条回复
切换为时间正序
请发表友善的回复…
发表回复
walkingpoison 2003-01-24
  • 打赏
  • 举报
回复
4200s?应该是4200ms吧?不然要一个多小时了
xizi2002 2003-01-24
  • 打赏
  • 举报
回复
的确跟长度有关,我在php用长一点(不是楼主所说的正则)的测600个文件用了4200s,用小一点的用了3000s.
当然这里面有出入,但是大致是这样的
xizi2002 2003-01-24
  • 打赏
  • 举报
回复
这么小问题,争什么呵?当然是第一个快了,呵呵
walkingpoison 2003-01-24
  • 打赏
  • 举报
回复
换一个思路,可以这么写:
return this.replace(/^\s*(\S*(?:\s+\S+)*)\s*$/,"$1");

速度也不慢。(ie5.5以前的版本将?:去掉,但是速度会有所下降)
saucer 2003-01-23
  • 打赏
  • 举报
回复
sorry, my trim2() was wrong, check out the following, trim1() is not slower:


<script>
function String.prototype.trim1()
{
return this.replace(/^\s+|\s+$/g, "");
}

function String.prototype.trim2()
{
return this.replace(/^\s*([^\s].*[^\s])\s*$/,"$1");
}


n = 10000;

s = " 1234 ";
alert("***" + s.trim1() + "***");
alert("***" + s.trim2() + "***");

dt = new Date();

for (i=0; i < n; i++)
s.trim1();
alert(new Date().getTime() - dt.getTime());

dt = new Date();

for (i=0; i < n; i++)
s.trim2();
alert(new Date().getTime() - dt.getTime());


s = "";
dt = new Date();

for (i=0; i < n; i++)
s.trim1();
alert(new Date().getTime() - dt.getTime());

dt = new Date();

for (i=0; i < n; i++)
s.trim2();
alert(new Date().getTime() - dt.getTime());

</script>
saucer 2003-01-23
  • 打赏
  • 举报
回复
it seems the two matchings cost dearly


<script>
function String.prototype.trim1()
{
return this.replace(/^\s+|\s+$/g, "");
}

function String.prototype.trim2()
{
return this.replace(/^\s+(.*)\s+$/,"$1");
}

n = 10000;

s = " 1234 ";
alert("***" + s.trim1() + "***");
alert("***" + s.trim2() + "***");

dt = new Date();

for (i=0; i < n; i++)
s.trim1();
alert(new Date().getTime() - dt.getTime());

dt = new Date();

for (i=0; i < n; i++)
s.trim2();
alert(new Date().getTime() - dt.getTime());


s = "";
dt = new Date();

for (i=0; i < n; i++)
s.trim1();
alert(new Date().getTime() - dt.getTime());

dt = new Date();

for (i=0; i < n; i++)
s.trim2();
alert(new Date().getTime() - dt.getTime());

</script>
allforly 2003-01-23
  • 打赏
  • 举报
回复
var s = "sspsspsspsspsspssp"
用正则怎么把第二个p和第四个p替换成w呢?
qiushuiwuhen 2003-01-23
  • 打赏
  • 举报
回复

还有下面的又在那个地位呢?

<script>
function String.prototype.trim(){
var s=this.search(/\S/)
var e=this.search(/\s*$/)
return this.substring(s,e)
}
s=" "+new Array(100).join("1234567890")+" ";
alert("["+s.trim()+"]");
s=" "
alert("["+s.trim()+"]");
s="a "
alert("["+s.trim()+"]");
s=" a"
alert("["+s.trim()+"]");
</script>

最后还是支持MS提供的第一种方法
qiushuiwuhen 2003-01-23
  • 打赏
  • 举报
回复
用测试判断,只是用来验证理论,还是先想想为好,而且测试所用数据也不够,
1.大数据量 s = " "+new Array(100).join("1234567890")+" ";
2.特殊情况 s = " ",第二种方法就开始错误了

继续问,哪个耗时多?
function String.prototype.trim1()
{
return this.replace(/^\s*(.*?)\s*$/,"$1");
}
function String.prototype.trim2()
{
return this.replace(/^\s*(.*\S?)\s*$/,"$1");
}
以及下面的比较
function String.prototype.trim1()
{
return this.replace(/^\s+|\s+$/g, "");
}

function String.prototype.trim2()
{
return this.replace(/^\s+/,"").replace(/\s+$/, "");
}



chjpeng 2003-01-23
  • 打赏
  • 举报
回复
感覺一次匹配應該要比兩次匹配快些吧
還是saucer的方法好,直接測試耗時來判斷

bxxxr(我用的是IE5):
script56.chm里面就有
emu 2003-01-23
  • 打赏
  • 举报
回复
function String.prototype.trim2()
{
return this.replace(/(^\s*(.*\S)\s*$|^\s*$)/,"$2");
}

这样速度上就没有很大优势了。
chenbinghui 2003-01-23
  • 打赏
  • 举报
回复
问题是:第二个正则表达式不正确!!!!
var test=" ";
alert("\""+test.trim2()+"\"");
qiushuiwuhen 2003-01-23
  • 打赏
  • 举报
回复
IE5.5+用,callback趋势
<script>
function pw(str){
var i=0;
return str.replace(/p/g,function (){return i++%2?"w":"p"})
}
alert(pw("sspsspsspsspsspssp"))
alert(pw("pppp"))
</script>
saucer 2003-01-23
  • 打赏
  • 举报
回复
>>>>这样又对付不了空串
why do you need to worry about 空串? if the expression doesn't match, the replacement never occurs

if you insist, try (some lower version IEs may not support .*?)

function String.prototype.trim2()
{
return this.replace(/^\s*(.*?\S?)\s*$/,"$1");
}
blues-star 2003-01-23
  • 打赏
  • 举报
回复
来晚了啊,呵呵
aviva77 2003-01-23
  • 打赏
  • 举报
回复
你上QQ或者给我打个电话吧。
我糟透了。
emu 2003-01-23
  • 打赏
  • 举报
回复
aviva,我没有收到你的信。
emu 2003-01-23
  • 打赏
  • 举报
回复
不行,
function String.prototype.trim2()
{
return this.replace(/^\s*(.*\S?)\s*$/,"$1");
}

虽然快,可是有错,因为中间的.*会贪心的把后面的空格给吃进来呀。该怎么写呢?

return this.replace(/^\s*(.*\S)\s*$/,"$1");

这样又对付不了空串。



saucer 2003-01-23
  • 打赏
  • 举报
回复
>>>>var s = "sspsspsspsspsspssp"
>>>>用正则怎么把第二个p和第四个p替换成w呢?

<script>
var re = /^([^p]*)(p)([^p]*)(\2)([^p]*)(\2)([^p]*)(\2)(.*)$/;
var rp = "$1$2$3w$5$6$7w$9";
var s = "sspsspsspsspsspssp";
alert(s)
s=s.replace(re,rp);
alert(s);
var s = "pppp";
alert(s)
s=s.replace(re,rp);
alert(s);
</script>
aviva77 2003-01-23
  • 打赏
  • 举报
回复
EMU我找你。
我给你写信没收到你的回信。
你今天打个电话给我好吗?
加载更多回复(13)

87,994

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧