English 中文(简体)
Javascript RegExp限定符问题
原标题:Javascript RegExp quantifier issue

我有一些运行的JavaScript使用正则表达式替换来修改页面上的内容。不过,我对一个特定的正则表达式量词有问题。我读过的所有文档(我知道它也可以用regex在其他语言中工作)都说JavaScript支持{N}{N,}{N,N}量词。也就是说,您可以指定所需的特定匹配数或匹配范围。例如,(zz){5,}在一行中至少匹配10个z s,并且z{5、10}将匹配从5到10的任何数量的z s,不多也不少。

问题是,我可以匹配一个确切的数字(例如z{5}),但不能匹配范围。我能想到的最接近的是,它与正则表达式字符串中的逗号有关,但我不明白为什么,也无法绕过这一点。我尝试过转义逗号,甚至使用unicode十六进制字符串表示逗号(u002C),但都无济于事。

为了消除任何可能的误解,并解决评论中提出的一些问题,这里有一些额外的信息(也可以在评论中找到):我已经尝试以所有可能的方式创建数组,包括var=[/z{5,}/gi,/a{4,5}/gi]var=[新的RegExp(z{5,},gi),新的RegExp(a{4,5},gi]),以及var[0]=new RegExp(z{5,}),gi)等。该数组在for循环中用作somevar.replace(regex[i],subst[i])

最佳回答

我想我已经想通了。我用各种方法构建数组以使其发挥作用,但我认为不同的是在正则表达式字符串周围使用单引号,而不是像[/z{5,}/,/t{7,9}/gi]那样将其打开。因此,当我执行[/z{5,}/,/t{7,9}/gi]时,似乎已经修复了它。尽管像Alan的例子一样,有时没有它们也能正常工作。只是我想不是这样。

问题回答

也许我误解了这个问题,但{n}运算符的Javascript实现似乎相当不错:

"foobar".match(/o{2,4}/); // => matches  oo 
"fooobar".match(/o{2,4}/); // => matches  ooo 
"foooobar".match(/o{2,4}/); // => matches  oooo 
"fooooooobar".match(/o{2,4}/); // => matches  oooo 
"fooooooobar".match(/o{2,4}?/); // => lazy, matches  oo 
"foooobar".match(/(oo){2}/); // => matches  oooo , and captures  oo 
"fobar".match(/[^o](o{2,3})[^o]/); // => no match
"foobar".match(/[^o](o{2,3})[^o]/); // => matches  foob  and captures  oo 
"fooobar".match(/[^o](o{2,3})[^o]/); // => matches  fooob  and captures  oo 
"foooobar".match(/[^o](o{2,3})[^o]/); // => no match

它对我有用。

var regex = [/z{5,}/gi,/a{4,5}/gi];
var subst = [ ZZZZZ , AAAAA ];
var somevar =  zzzzz aaaaa aaaaaaa zzzzzzzzzz aaazzzaaaaaa ;
print(somevar);
for (var i=0; i<2; i++) {
    somevar = somevar.replace(regex[i], subst[i]);
}
print(somevar);

输出:

zzzzz aaaaa aaaaaaa zzzzzzzzzz aaazzzaaaaaa
ZZZZZ AAAAA AAAAAaa ZZZZZ aaazzzAAAAAa

构造函数版本也适用:

var regex = [new RegExp( z{5,} , gi ),new RegExp( a{4,5} , gi )];

在ideone.com上看到它的实际效果





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.