0

Given the following string:

Lorem {{ipsum}} dolor {{sit}} amet

I'm trying to extract thw words ipsum and sit with the following regex:

content = 'Lorem {{ipsum}} dolor {{sit}} amet'
var regexp = /^\\\{\\\{(\w)\\\}\\\}/g;
var match = regexp .exec(content);

The match object returns null. What am I missing? Thanks!

3
  • 1
    What makes {{ipsum}} different from {{sit}}? And what makes amet different from Lorem and dolor? Commented Sep 14, 2013 at 10:31
  • @Sumurai8 bot ipsum and sit are captured into {{ and }}. There was a mistake in the question. I'm trying to extract the words ipsum and sit :) Commented Sep 14, 2013 at 10:38
  • You only need to double backslashes if you're creating the regep from a string literal, not a regexp literal. Commented Sep 14, 2013 at 10:40

1 Answer 1

2

You have WAY too many backslashes, you're only looking for a single word character, and you're only looking for matches right at the beginning of the string.

var regexp = /\{\{(\w+)\}\}/g;
Sign up to request clarification or add additional context in comments.

3 Comments

The OP doesn't want the two words inside the {{ and }} !! I think that is what he wants but he has a typo but can't be sure.
@Sniffer He's fixed the typo, he does want the words inside {{}}.
@Kolink Thanks! it seems this works. So I falsely assumed that: \w covers whole words instead of only chars and the ` should be also escaped, thinking which led to \\\{`

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.