首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将Java Regex转换为javascript regex

将Java Regex转换为javascript regex
EN

Stack Overflow用户
提问于 2014-04-03 13:32:28
回答 3查看 14K关注 0票数 7
代码语言:javascript
复制
([a-zA-Z0-9_\\-])([a-zA-Z0-9_\\.+~!#/$%^&*_=\\'?\\-]*)@[A-Za-z0-9-]+(\\.[A-Za-z0-9-]+)*(\\.[A-Za-z0-9]{2,})$

这是工作良好的Java,但它不工作的JavaScript可能是反斜杠有一些问题,请告诉我如何才能转换上面的java正则表达式到Java脚本。

EN

回答 3

Stack Overflow用户

发布于 2014-04-03 13:39:11

只需将双反斜杠减少为单斜杠。此外,如果连字符是字符类中的最后一个字符,则不需要转义连字符。此外,您也不需要在字符类中转义通配符

像这样的东西

代码语言:javascript
复制
/([a-zA-Z0-9_-])([a-zA-Z0-9_.+~!#/$%^&*_='?-]*)@[A-Za-z0-9-]+(\.[A-Za-z0-9-]+)*(\.[A-Za-z0-9]{2,})$/
票数 9
EN

Stack Overflow用户

发布于 2014-04-03 13:40:06

这取决于你是如何使用它的?用什么代码?

当你有两个\时,你可能只需要一个\\

代码语言:javascript
复制
var re = new RegExp("ab+c");

代码语言:javascript
复制
var re = /ab+c/;

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions

票数 1
EN

Stack Overflow用户

发布于 2014-04-03 13:40:27

Regex Demo

代码语言:javascript
复制
var myregexp = /([a-zA-Z0-9_\-])([a-zA-Z0-9_\.+~!#\/$%^&*_=\'?\-]*)@[A-Za-z0-9-]+(\.[A-Za-z0-9-]+)*(\.[A-Za-z0-9]{2,})$/;

Debuggex Demo

描述

代码语言:javascript
复制
1st Capturing group ([a-zA-Z0-9_\-])
    [a-zA-Z0-9_\-] match a single character present in the list below
        a-z a single character in the range between a and z (case sensitive)
        A-Z a single character in the range between A and Z (case sensitive)
        0-9 a single character in the range between 0 and 9
        _ the literal character _
        \- matches the character - literally
2nd Capturing group ([a-zA-Z0-9_\.+~!#\/$%^&*_=\'?\-]*)
    [a-zA-Z0-9_\.+~!#\/$%^&*_=\'?\-]* match a single character present in the list below
        Quantifier: Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
        a-z a single character in the range between a and z (case sensitive)
        A-Z a single character in the range between A and Z (case sensitive)
        0-9 a single character in the range between 0 and 9
        _ the literal character _
        \. matches the character . literally
        +~!# a single character in the list +~!# literally
        \/ matches the character / literally
        $%^&*_= a single character in the list $%^&*_= literally (case sensitive)
        \' matches the character ' literally
        ? the literal character ?
        \- matches the character - literally
    @ matches the character @ literally
[A-Za-z0-9-]+ match a single character present in the list below
    Quantifier: Between one and unlimited times, as many times as possible, giving back as needed [greedy]
    A-Z a single character in the range between A and Z (case sensitive)
    a-z a single character in the range between a and z (case sensitive)
    0-9 a single character in the range between 0 and 9
    - the literal character -
3rd Capturing group (\.[A-Za-z0-9-]+)*
    Quantifier: Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
    Note: A repeated capturing group will only capture the last iteration. Put a capturing group around the repeated group to capture all iterations or use a non-capturing group instead if you're not interested in the data
    \. matches the character . literally
[A-Za-z0-9-]+ match a single character present in the list below
    Quantifier: Between one and unlimited times, as many times as possible, giving back as needed [greedy]
    A-Z a single character in the range between A and Z (case sensitive)
    a-z a single character in the range between a and z (case sensitive)
    0-9 a single character in the range between 0 and 9
    - the literal character -
4th Capturing group (\.[A-Za-z0-9]{2,})
    \. matches the character . literally
    [A-Za-z0-9]{2,} match a single character present in the list below
        Quantifier: Between 2 and unlimited times, as many times as possible, giving back as needed [greedy]
        A-Z a single character in the range between A and Z (case sensitive)
        a-z a single character in the range between a and z (case sensitive)
        0-9 a single character in the range between 0 and 9
    $ assert position at end of the string
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22828644

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档