XPages validateConstraint 正则表达式对于网络来说很奇怪

XPages validateConstraint regex acting strange for web

我有一个输入字段 email1,我想将其与正则表达式一起使用以进行验证。当我在网络上加载 xpage 时,它​​会将反斜杠添加到现有反斜杠的前面(我认为我应该在代码中这样做)。它还用我认为的 unicode 替换了非字母数字。

<xp:td>
  <xp:inputText value="#{document1.email}" id="email1"
       required="true" styleClass="form-control">
    <xp:this.attrs>
      <xp:attr name="placeholder"
        value="Enter your email address">
      </xp:attr>
    </xp:this.attrs>
    <xp:this.validators>
      <xp:validateRequired
        message="Email is required">
      </xp:validateRequired>
      <xp:validateConstraint
        message="email not valid">
          <xp:this.regex><![CDATA[/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i]]></xp:this.regex>
      </xp:validateConstraint>
    </xp:this.validators>
  </xp:inputText>
</xp:td>

这是渲染后的 html:

XSP.addOnLoad(function() {
XSP.attachValidator("view:_id1:_id2:email1",new XSP.RequiredValidator("Email is required"),null,new XSP.RegExpValidator("/^\u0028[\w-]+\u0028?:\.[\w-]+\u0029*\u0029@\u0028\u0028?:[\w-]+\.\u0029*\w[\w-]{0,66}\u0029\.\u0028[a-z]{2,6}\u0028?:\.[a-z]{2}\u0029?\u0029$/i","email not valid"));

我试过使用两种形式的正则表达式值,使用静态值和计算值,都失败了。

<xp:this.regex><![CDATA[#{javascript:/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i}]]></xp:this.regex>

谁能看出我做错了什么并指出正确的方向。

去掉正则表达式开头的“/”和末尾的“/i”。然后就可以了

<xp:this.regex><![CDATA[^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$]]></xp:this.regex>

领先的“/”已经包含在内部,不幸的是像“/i”这样的修饰符(不区分大小写的匹配)不似乎有效。

不用担心呈现的 JavaScript 代码中的双反斜杠和 unicode 字符。它们将被 JavaScript 解释为一个反斜杠和原始字符。