1

I've the pleasure to find all strings in our projects which are not angularjs expressions because we're going multi language (so every string which is not fully between curly braces).

What I wanna do is build a regex which matches all strings, which have no angular expressions (or part of the string is no angular expression).

The var names describe which should match (yes) and which shouldn't (nope).

var yes = "test";
var nope = "{{xyz}}";
var yes = "test {{xyz}}";
var nope = "{{::xyz}}";
var nope = "{{xyz}} {{abc}}"; //as whitespace is okay

Tried a lot of different stuff using negative lookaheads etc. but ended up with a not even close working regex.

"([^}}])+{{|"$

Maybe somebody can help me, as my head is like to explode...

Regex101: https://regex101.com/r/VePtVp/1

2
  • 1
    Try ^[a-z]+(?: +\{\{[^}]+\}\})?$ here. Commented Sep 6, 2018 at 13:59
  • @UnbearableLightness was indeed working for my few examples, but didnt work for eg. "{{xyz}} test {{abc}}" or "{{xyz}} edit". Sorry for that, should've added some more examples. Thanks anyway :) Commented Sep 7, 2018 at 10:15

2 Answers 2

1

You may use following regex for match:

/"(?:\s*{{[^\s}]*}}\s*)+"/

RegEx Demo

RegEx Details:

  • ": Match start quote
  • (?:: Start non-capture group
    • \s*{{[^\s}]*}}\s*: Match {{...}} string surrounded by optional whitespaces.
  • )+: End non-capture group. + matches 1 or more of this group
  • ": Match end quote
Sign up to request clarification or add additional context in comments.

3 Comments

Awesome, thank you :). Needed the yes to match, though. So I edited the regex a bit and it's working now.
To match yes variables use: "(?!(?:\s*{{[^\s}]*}}\s*)+").+" or check this demo: regex101.com/r/VePtVp/6
Ha awesome, we nearly ended up with the same regex. Thank you again :).
0

Thanks to anubhava as his post and explanation helped me creating this regex here:

"(?!(\{\{[^\s]*\}\}\s*)+").*"

var yes = "test";
var nope = "{{xyz}}";
var yes = "test {{xyz}}";
var yes = "{test";
var yes = "{test} bearbeiten";
var yes = "{test}";
var nope = "{{::xyz}}";
var nope = "{{xyz}} {{abc}}";
var yes = "{{xyz}} test {{abc}}";
var yes = "{{xyz}} test {{abc}} temp {{var}}";

https://regex101.com/r/r9lmDL/2

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.