douhua1760 2014-06-28 12:04
浏览 31
已采纳

从Javascript到PHP的正则表达式[关闭]

I Want to know if there is a tool for converting JavaScript Regex to PHP.

I got the following Regex in JAvascript

 var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;

And

 pwbx2.match(/.[^!,+,§,\),\(,=,\-,\.,:,,,\w]/)

Now I wan't to match the given values to check also at the server side on this regex. How can i do it?

Cheers

Sven

  • 写回答

2条回答 默认 最新

  • dongyu4908 2014-06-28 23:25
    关注

    You need to know that .[^!,+,§,\),\(,=,\-,\.,:,,,\w] is a monstrosity. In normal regex, this would be:

    .[^-.,:+=!§()\w]
    

    The [brackets] indicate a character class. They mean "match one character in this class," or, when the first charcter is ^ "match one character that is not in this class." So there is never any need to repeat a character in a character class. This one has eleven commas!

    Your test could be expressed in this compact fashion:

    $theregex = '~.[^-.,:+=!§()\w]~';
    echo (preg_match($theregex, $yourstring)) ? "**It Matches!**" : "Nah... No match." ;
    

    But that probably doesn't work the way you want anyway

    The original regex begs the question: what where they thinking when they wrote this? Clearly whoever wrote this was regex-illiterate (and I don't mean to be gratuitously condescending... that's just a fact.) Therefore, it is quite likely that they had something quite different in mind when they wrote it. And if that's a case, it doesn't matter how well you translate it: it won't do the job it was intended for.

    If you know what the regex is supposed to do, tell us, and we'll help you fix it.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料