regex: ignore between brackets and parentheses

2.2k views Asked by At

I have a regex that checks that a phrase is included:

var regex = new RegExp( "(\\s|\\b)" + phrases[i] + "(\\s|\\b)", "i" );
var result = regex.test( value );

What I need it to do is ignore the phrase if it's between brackets [] AND parentheses ().

I'm actually using Markdown to generate the HTML, but I don't want it to accept the phrase if it's in a Link or Image markdown syntax.

Example:

Requiring the phrase 'test', it will accept:

dfg dfg df [gdf](http://test123.com "test") gdfg df gdfg

Because 'test' is in the link title.

If I can get it to ignore the phrase inside the syntax [...](...) it would be awesome.

If not, just ignoring anything between brackets or parentheses would work.

1

There are 1 answers

0
Joseph Marikle On BEST ANSWER

You can parse out brackets followed by parenthesis with this regex.

var string = 'dfg dfg df [gdf](http://test123.com "test") gdfg df gdfg'
string.replace(/\[.*\][(].*[)]/g,'');