user1275375
user1275375

Reputation: 1391

Retrieving parameter of a function using regular expressions in javascript

for example if we consider document.createElement() function the parameter can be passed in 3 ways

var v="script"; var s=document.createElement(v);

var s=document.createElement("script");

var s=document.createElement('scipt');

i want a regular expression which extracts the parameter in document.createElement function excluding quotes. I tried this by using groups but i am writing two regular expression one for "",'' and other for normal variable please provide an example

Upvotes: 0

Views: 80

Answers (2)

mihai
mihai

Reputation: 38543

var re = /document\.createElement\((['"]*)(.+?)\1\)/;

The result is in:

str.match(re)[2];

http://jsfiddle.net/mihaifm/RWc8N/

Upvotes: 1

Ofer Zelig
Ofer Zelig

Reputation: 17498

Use pipe - | for OR operations.

For example: script|scipt.

Upvotes: 0

Related Questions