1

I have this regex:

/(\[.*?\])/g

Now I want to change that regex to matches everything except current-matches. How can I do that?

For example:

Current regex:

   here is some text [anything123][/21something] and here is too [sometext][/afewtext] and here

//                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^                 ^^^^^^^^^^^^^^^^^^^^^

I want this:

   here is some text [anything123][/21something] and here is too [sometext][/afewtext] and here
// ^^^^^^^^^^^^^^^^^^                           ^^^^^^^^^^^^^^^^^                     ^^^^^^^^^
stack
  • 10,280
  • 19
  • 65
  • 117
  • 2
    Just use `replace` to replace the matches. `str.replace(/(\[.*?\])/g, '')` – Tushar Feb 28 '16 at 12:57
  • @Tushar I want to know is there any approach to match everything except pattern in regex? – stack Feb 28 '16 at 12:58
  • I would follow Tushar's suggestion using replace, that's what I was going to say. – Veverke Feb 28 '16 at 13:09
  • 1
    [`\[[^\]]+\](?:^|\s)([\w']+)(?!\])\b|(?:^|\s)([\w']+)(?!\])\b`](https://regex101.com/r/gU1nY6/2). See [Javascript Regex for all words not between certain characters](http://stackoverflow.com/questions/10448173/javascript-regex-for-all-words-not-between-certain-characters) – Tushar Feb 28 '16 at 13:21
  • `text.split(/\[.*?\]/g)` – georg Feb 28 '16 at 13:37

1 Answers1

2

Match what's inside or capture what's outside the trick

\[.*?\]|([^[]+)

See demo at regex101

Demo:

var str = 'here is some text [anything123][/21something] and here is too [sometext][/afewtext] and here';

var regex = /\[.*?\]|([^[]+)/g;
var res = '';

// Do this until there is a match
while(m = regex.exec(str)) {
    // If first captured group present
    if(m[1]) {
        // Append match to the result string
        res += m[1];
    }
}

console.log(res);
document.body.innerHTML = res; // For DEMO purpose only
Tushar
  • 85,780
  • 21
  • 159
  • 179
bobble bubble
  • 16,888
  • 3
  • 27
  • 46
  • 1
    @stack This does the same thing. You just need to get the first captured group and join them. – Tushar Feb 28 '16 at 13:06
  • @Tushar Yeah I know the content of `$1` is exactly what I need. btw, I can do that exactly as you said *(using `.replace()`)*, But to be honest, I want to know how can I do that using lookahead `(?!)`? *(Because I saw an approach using lookahead somewhere)* – stack Feb 28 '16 at 13:08
  • @Tushar Thank you .. yes it works ... But I think I cannot tell you what I want exactly .. `:-)` !! All I want to know is: *how do I define a regex to matches everthing except its pattern (pure regex)* – stack Feb 28 '16 at 13:12
  • 1
    @stack [you want this](https://regex101.com/r/nT7nV0/1) but this does not work in JS regex :p – bobble bubble Feb 28 '16 at 13:15
  • @stack Because that are [PCRE verbs](http://perldoc.perl.org/perlre.html#Special-Backtracking-Control-Verbs). [You could try like this with lookaheads](https://regex101.com/r/vE1iU2/1) but this is expensive and more prown to fail. – bobble bubble Feb 28 '16 at 13:22