Patrick
Patrick

Reputation: 5592

Regex to filter away certain groups of characters

I am trying to construct a regexp that goes through a string and return matches that are not part of markup code.

Tried different things so far but as soon as the markup is {{ }} it ignores the {{}} but returns the words inside the brackets.

(\w+(?<!{{.}}))

Example text:

apa {{abc}} banan apple {{def}} citrus apelsin {{ghi}} mango korsbar

I want it to return the following 4 matched groups:

apa
banan apple
citrus apelsin
mango korsbar

If that above is not possible then the following 7 matched groups would also work:

apa
banan
apple
citrus
apelsin
mango
korsbar

Sort of the same as when replacing markup with html but instead of replace it should extract everything but the markup.

(The markup code can of course change so it needs to be something that removes anything with {{something}})

Upvotes: 1

Views: 77

Answers (1)

Tushar
Tushar

Reputation: 87203

The string could be split by the {{.*?}} regex. This will get the strings other than those in two curly brackets in an array.

The regex is lazy, so it'll stop at first occurrence of }}.

str.split(/{{.*?}}/)

var str = 'apa {{abc}} banan apple {{def}} citrus apelsin {{ghi}} mango korsbar';

var arr = str.split(/{{.*?}}/);
console.log(arr);

To remove the spaces around the strings, use the regex

\s*{{.*?}}\s*

\s* here will match zero or more spaces around the bracketed strings and remove it.

var str = 'apa {{abc}} banan apple {{def}} citrus apelsin {{ghi}} mango korsbar';

var arr = str.split(/\s*{{.*?}}\s*/);
console.log(arr);

Upvotes: 2

Related Questions