简体   繁体   English

包含两个方括号的简单 JavaScript 正则表达式

[英]Simple JavaScript Regex that contains both square brackets

I'm trying to write a Regex of the form: najfo359f+-q3f#~[+0asa123123]a'/.df;ekfm345我正在尝试编写以下形式的正则表达式:najfo359f+-q3f#~[+0asa123123]a'/.df;ekfm345

  • Any character including numbers 0 or more times,任何包含数字 0 次或多次的字符,
  • 1 OPENING SQUARE BRACKET, 1 个开口方括号,
  • Any character including numbers 0 or more times,任何包含数字 0 次或多次的字符,
  • A NUMBER,一个号码,
  • 1 CLOSING SQUARE BRACKET, 1 个闭合方括号,
  • and any character including numbers 0 or more times,以及任何包含数字 0 次或多次的字符,

    This is what I have so far:这是我到目前为止所拥有的:

     /.*[.*0*/d+].*/

    But I can't match the simple [2245] and when I try to escape \ the opening square bracket my code doesn't execute.但是我无法匹配简单的[2245]并且当我尝试转义\左方括号时,我的代码不会执行。

    What am I missing?我错过了什么?

  • A few observations:几点观察:

    1. The 0 in the middle of your regex seems a bit arbitrary and doesn't appear to be considered in any of your criteria.正则表达式中间的0似乎有点随意,并且在您的任何标准中似乎都没有考虑。
    2. Unsure of why you're including a /d , maybe you meant \d ?不确定为什么要包含/d ,也许您的意思是\d A backslash \ is used to indicate escape sequences.反斜杠\用于指示转义序列。

    You're looking for something more akin to:您正在寻找更类似于:

    /.*\[.*\d+\].*/
    

    Regex101正则表达式101

    In context with Javascript:在 Javascript 的上下文中:

     const pattern = /.*\[.*\d+\].*/; console.log(pattern.test("najfo359f+-q3f#~[+0asa123123]a'/.df;ekfm345")); console.log(pattern.test("[2245]"));

    声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

     
    粤ICP备18138465号  © 2020-2024 STACKOOM.COM