4
I’m wanting to do with regex (regular expression), for example (if it’s javascript):
var str = '[abc\[0123\]] [efg\[987\]h] [olá \[mundo\]!] [foo [baz]]';
str.match(/\[(.*?)\]/g);
Exit:
["[abc[0123]", "[efg[987]h", "[olá [mundo]!", "[foo [baz]"]
Or
var str = '{abc\{0123\}} {efg\{987\}h} {olá \{mundo\}!} {foo {baz}}';
str.match(/\{(.*?)\}/g);
Exit:
["{abc{0123}", "{efg{987}", "{olá {mundo}", "{foo {baz}"]
But I would like the first to ignore places not escaped as [foo [baz]]
and just take the [baz]
and the escapees, like this:
["[abc[0123]]", "[efg[987]h]", "[olá [mundo]!]", "[baz]"]
And in the second it returns:
{"{abc{0123}}", "{efg{987}h}", "{olá {mundo}!}", "{baz}"]
My intention initially is for study, but I also intend to use in things like a structure that is similar to CSS selectors, so for example:
input[name=\[0\]], input[name=foo\[baz\]\[bar\]]
It would return this:
[0], [1]
Or a map of Urls I intend to create:
/{nome}/{foo\{bar}/{baz\{foo\}}/
And return this:
{nome}, {foo{bar}, {baz{foo}}
What I want is to ignore the escaped characters, how can I do this? Can provide an example in any language, the most important is Regex
To ignore spaced characters you can use:
[^\\]+
. You want the return of this regex to be N Groups?– Gabriel Gonçalves
@Gabrielgonçalves works well, but he is accepting the not escaped too, for example
var str = '{abc{0123}}'; /\{([^\\]+)\}/.exec(str);
– Guilherme Nascimento