skip to Main Content

Test string:

*This* is a test.[15] I would like[16] To figure _out how_ to do this.[20]

RegEx:

/[d+]|*.**|_.*_/gu

Actual Results:

Match 1: *This*
Match 2: [15]
Match 3: [16]
Match 4: _out how_
Match 5: [20]

Desired Results:

Match 1: *This*
Match 2:  is a test.
Match 3: [15]
Match 4:  I would like
Match 5: [16]
Match 6:  To figure 
Match 7: _out how_
Match 8:  to do this.
Match 9: [20]

2

Answers


  1. You may consider this approach that splits the input with capture group and uses filter to remove empty match from the start.

    const s = '*This* is a test.[15] I would like[16] To figure _out how_ to do this.[20]'
    console.log( s.split(/(*[^*]**|_[^_]+_|[d+])/).filter(Boolean) );
    
    /*
    [
      "*This*",
      " is a test.",
      "[15]",
      " I would like",
      "[16]",
      " To figure ",
      "_out how_",
      " to do this.",
      "[20]"
    ]
    */

    RegEx Details:

    • (: Start capture group
      • *[^*]**: Match substring wrapped in *
      • |: OR
      • _[^_]+_: Match substring wrapped in _
      • |: OR
      • [d+]: Match [number] part
    • ): End capture group
    Login or Signup to reply.
  2. If you are looking for a pure regex implementation.
    In your current regex, you have not included matching for the "non-special" words so they are not showing up in the match.

    Only modifying the regex you already have. you would need something as follows

    /[d+]|*.**|_.*_|.+?(?=[|_|*)/gu
    

    Here the addition of .+? is a non-greedy match until the regex encounters any of the characters mentioned in the positive lookahead (?=[|_|*) which is ‘[‘ or ‘_’ or ‘*’ set based on your example.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search