0

I have a String like var foo = 'foo {{Email18}}'. But the digit part is variable or unknown.

I need to get the subString {{Email18}}.

I want to do something like foo.search(/{{Email\d{1,}}}/) which will return me {{Email18}}. How do I do that?

1
  • As a side note, the quantifier + is semantically equivalent to your quantifier {1,}. Commented May 25, 2016 at 8:47

2 Answers 2

3

Use match() with regex /{{Email\d{1,}}}/

var foo = 'foo {{Email18}}';

console.log(
  foo.match(/{{Email\d{1,}}}/)[0]
);

Sign up to request clarification or add additional context in comments.

Comments

1

Or you can use a little string manipulation:

var str = "foo {{Email18}}";

function myFunction(arg) {
    return arg.toString().substr(arg.indexOf("{"), arg.lastIndexOf("}"));
}; 

var newStr = myFunction(str);

2 Comments

Nice answer, but I don't see the point of using a function here if you don't pass the string as a parameter.
@Aaron: Here you go! :)

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.