Patrick Patrick - 1 month ago 7
Javascript Question

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}})

Answer

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);

Comments