Nitin Aggarwal
Nitin Aggarwal

Reputation: 215

How to get a substring javascript

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?

Upvotes: 0

Views: 189

Answers (2)

Ciprianis
Ciprianis

Reputation: 255

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

Upvotes: 1

Pranav C Balan
Pranav C Balan

Reputation: 115212

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

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

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

Upvotes: 3

Related Questions