正则表达式 - 匹配除最后一个之外的所有 [英] Regular Expression - Match all except last one

查看:342
本文介绍了正则表达式 - 匹配除最后一个之外的所有的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道/ g标志将匹配所有出现的内容。有没有办法,用

a正则表达式来匹配所有出现*除了*最后一个?


pattern = / df / g;

var myString =" asdfasdfasdfasdf" ;;

var newString = myString.replace(pattern,''gh'');

alert(newString)


给我:asghasghasghasgh应该如此。

我想要的是:asghasghasghasdf最后一个没有被替换的地方。


-

兰迪

comp.lang.javascript常见问题 - http://jibbering.com/faq &新闻组每周

Javascript最佳实践 - http://www.JavascriptToolbox .com / bestpractices /

I know that the /g flag will match all occurrences. Is there a way, with
a Regular Expression, to match all occurrences *except* the last one?

pattern = /df/g;
var myString = "asdfasdfasdfasdf";
var newString = myString.replace(pattern,''gh'');
alert(newString)

Gives me: asghasghasghasgh as it should.
What I want: asghasghasghasdf Where the last one is not replaced.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/

推荐答案

> Randy Webb <您好************ @ aol.com>写道:
> "Randy Webb" <Hi************@aol.com> wrote:
新闻:Q7 ******************** @ comcast.com ....

我知道/ g标志将匹配所有出现的事件。有没有一种方法,
与正则表达式,以匹配所有出现*除了*
最后一个?

pattern = / df / g;
var myString =" asdfasdfasdfasdf" ;;
var newString = myString.replace(pattern,''gh'');
alert(newString)

给我:asghasghasghasgh应该如此。
我想要的是:asghasghasghasdf最后一个没有被替换的地方。
news:Q7********************@comcast.com....

I know that the /g flag will match all occurrences. Is there a way,
with a Regular Expression, to match all occurrences *except* the
last one?

pattern = /df/g;
var myString = "asdfasdfasdfasdf";
var newString = myString.replace(pattern,''gh'');
alert(newString)

Gives me: asghasghasghasgh as it should.
What I want: asghasghasghasdf Where the last one is not replaced.




字符串是否总是以模式结束?


-

BootNic 2006年3月19日星期日上午1:19


这并不是说有些人有意志力,有些人不会。这是

有些人准备改变而其他人则没有。

* James Gordon *



Does the string always end with the pattern?

--
BootNic Sunday, March 19, 2006 1:19 AM

It''s not that some people have willpower and some don''t. It''s that
some people are ready to change and others are not.
*James Gordon*


BootNic在3/19/2006 1:20 AM上发表以下内容:
BootNic said the following on 3/19/2006 1:20 AM:
" Randy Webb" <您好************ @ aol.com>写道:
新闻:Q7 ******************** @ comcast.com ....

我知道/ g标志将匹配所有出现。有没有一种方法,
与正则表达式,以匹配所有出现*除了*
最后一个?

模式= / df / g;
var myString =" asdfasdfasdfasdf";
var newString = myString.replace(pattern,''gh'');
alert(newString)

给我:asghasghasghasgh应该如此。
我想要的是:asghasghasghasdf最后一个没有被替换的地方。
"Randy Webb" <Hi************@aol.com> wrote:
news:Q7********************@comcast.com....

I know that the /g flag will match all occurrences. Is there a way,
with a Regular Expression, to match all occurrences *except* the
last one?

pattern = /df/g;
var myString = "asdfasdfasdfasdf";
var newString = myString.replace(pattern,''gh'');
alert(newString)

Gives me: asghasghasghasgh as it should.
What I want: asghasghasghasdf Where the last one is not replaced.



字符串总是以模式结束吗?



Does the string always end with the pattern?




是的。而且我想我知道你要去哪里但是我会等着看看:)


我实际匹配的模式是\\\\ n,字符串是.value

的textarea。它将替换为; \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ $ b为他们创建document.write语句。我最后得到的结果

是一个额外的document.write(我不想要的。

我总是可以直接替换document.write的最后一次出现(但是我/ b $ b希望有一个更简单的解决方案。

-

Randy

comp.lang.javascript常见问题 - http: //jibbering.com/faq & newsgroup每周

Javascript最佳实践 - http://www.JavascriptToolbox.com/bestpractices/



Yes. And I think I know where you are headed but I will wait and see :)

The pattern I am actually matching is \r\n and the string is the .value
of a textarea. It is replacing it with ";\r\ndocument.write("
Where the " is part of the replacement. All it does is take code and
create document.write statements for them. What I end up with at the end
is an extra document.write(" that I don''t want.

I could always just replace the last occurence of document.write(" but I
was hoping there was a simpler solution.
--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/


>" Randy Webb"< Hi *** *********@aol.com>写道:
> "Randy Webb" <Hi************@aol.com> wrote:
新闻:qZ ******************** @ comcast。 COM .... <无线电通信/>
BootNic在3/19/2006 1:20 AM上发表以下内容:
news:qZ********************@comcast.com....

BootNic said the following on 3/19/2006 1:20 AM:
" Randy Webb" <您好************ @ aol.com>写道:
新闻:Q7 ******************** @ comcast.com ....

我知道/ g标志将匹配所有出现。是否有一个
方式,使用正则表达式来匹配所有出现*除了*
最后一个?

pattern = / df / g;
var myString =" asdfasdfasdfasdf";
var newString = myString.replace(pattern,''gh'');
alert(newString)

给我:asghasghasghasgh应该如此。
我想要的是:asghasghasghasdf最后一个没有被替换的地方。
"Randy Webb" <Hi************@aol.com> wrote:
news:Q7********************@comcast.com....

I know that the /g flag will match all occurrences. Is there a
way, with a Regular Expression, to match all occurrences *except*
the last one?

pattern = /df/g;
var myString = "asdfasdfasdfasdf";
var newString = myString.replace(pattern,''gh'');
alert(newString)

Gives me: asghasghasghasgh as it should.
What I want: asghasghasghasdf Where the last one is not replaced.



字符串总是以模式结束吗?



Does the string always end with the pattern?



是的。而且我想我知道你要去哪里但是我会等着看

我实际匹配的模式是\\\\ n,字符串是
。文本区域的价值。它将替换为; \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\我最终得到的是
结尾是一个额外的document.write(我不想要的。

我总是可以替换文档的最后一次出现。写(
但我希望有一个更简单的解决方案。



Yes. And I think I know where you are headed but I will wait and
see :)

The pattern I am actually matching is \r\n and the string is the
.value of a textarea. It is replacing it with ";\r\ndocument.write("
Where the " is part of the replacement. All it does is take code and
create document.write statements for them. What I end up with at
the end is an extra document.write(" that I don''t want.

I could always just replace the last occurence of document.write("
but I was hoping there was a simpler solution.




现在,我不认为我理解这种情况。听起来好像

你需要在

替换之前去除刺痛两端的空白区域。


但是我不要以为这就是问题。


我要建议两件事之一。


pattern = / df(? =。)/ g;

var myString =" asdfasdfasdfasdf" ;;

var newString = myString.replace(pattern,''gh'');

alert(newString);





var myString =" asdfasdfasdfasdf";

while(myString.match(/ df / g)。length!= 1){

myString = myString.replace(/ df / ,''gh'')

}

alert(myString);


-

BootNic 2006年3月19日星期日2:21

首先获取您的信息,然后您可以歪曲它们

please。

* Mark Twain *



Well now, I don''t think I am comprehending the situation. It sounds like
you need to strip the the white space on each end of the sting before the
replace.

But I don''t think that is the issue.

I was going to suggest one of two things.

pattern = /df(?=.)/g;
var myString = "asdfasdfasdfasdf";
var newString = myString.replace(pattern,''gh'');
alert(newString);

Or

var myString = "asdfasdfasdfasdf";
while(myString.match(/df/g).length!=1){
myString=myString.replace(/df/,''gh'')
}
alert(myString);

--
BootNic Sunday, March 19, 2006 2:21 AM

Get your facts first, and then you can distort them as much as you
please.
*Mark Twain *


这篇关于正则表达式 - 匹配除最后一个之外的所有的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆