简体   繁体   中英

Simple JavaScript Regex that contains both square brackets

I'm trying to write a Regex of the form: najfo359f+-q3f#~[+0asa123123]a'/.df;ekfm345

  • Any character including numbers 0 or more times,
  • 1 OPENING SQUARE BRACKET,
  • Any character including numbers 0 or more times,
  • A NUMBER,
  • 1 CLOSING SQUARE BRACKET,
  • and any character including numbers 0 or more times,

    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.

    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.
    2. Unsure of why you're including a /d , maybe you meant \d ? A backslash \ is used to indicate escape sequences.

    You're looking for something more akin to:

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

    Regex101

    In context with Javascript:

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

    The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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