0

我有以下文字:

Title %%% info@mydomain.com

我有以下脚本:

update: function(){
    this.AjaxImage(this.mainImage.current);
    // /[$-/:-?{-~!"^_`\[\]]/ Updated 05.22.10, changed .replace(/%%%[^%]*/,' ') to .replace(/%%%.*/,' ') because an escaped space (%20) was causing markup to appear on the page. DE 
    // only show the title and year below the image, %%% is the delimiter
    var caption = this.detailBin[this.mainImage.current]
                      .innerHTML.replace(/%%%.*/,' '); 
    this.overlayCaption('hide');
    this.controls.counter.update(this.mainImage.current+1);
    this.utilities.updateHash(this.mainImage.current+1);
    this.captionUnderlay.update(caption);

    // show everything under "more info"
    this.captionText = this.detailBin[this.mainImage.current]
                           .innerHTML.replace('%%%',' '); 
    this.hasMoreInfo = (this.captionText.length > caption.length+9) ? true : false;
    if(!this.hasMoreInfo) 
        this.controls.captionToggle.hide();
    else 
        this.controls.captionToggle.show();
}

this.captionUnderlay.update(this.detailBin[this.currentImage]
                                .innerHTML.replace(/%%%[^@]*/," "));

上面的标题Underlay 将显示@mydomain.com

我可以使用下面的 kludge 解决问题,但我想了解问题所在(我正在接管其他人编写的代码)。

如果我[^@]从正则表达式中删除,它会显示所有内容。如果我用它代替[^@][^}],除非我}在文本中有一个。

如何防止这种情况发生?

4

1 回答 1

2
.replace(/%%%[^@]*/," ")

正在寻找%%%后跟 0 个或多个不是@. 给定字符串"Title %%% info@mydomain.com"- 这意味着它找到%%% info(因为在 info 之后有一个 @ 符号),然后将其替换为空格 ( ," ")。制作字符串"Title @mydomain.com"

如果您只想要,代码顶部的表达式实际上是正确的"Title "

.replace(/%%%.*/,' ')

.因为这会在 3 个百分号之后找到任何字符 ( ) 0 次或更多次。然而,这确实留下了空间Title- 为了纠正这个问题,我们将使用下面的表达式进行完全修剪的返回:

固定表达式

.replace(/\s*%%%.*/,'')
于 2013-09-10T16:28:58.720 回答