Retrieving parameter of a function using regular expressions in javascript

Go To StackoverFlow.com

0

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

2012-04-04 07:33
by user1275375
You're going to parse JavaScript code with regexes?! Better wrap document.createElement to make hook. Also, script may be inserted via document.body.innerHTML += "<script type='text/javascript'></script>" - kirilloid 2012-04-04 07:37
what does that mean wrapping document.createElement? please explai - user1275375 2012-04-04 08:14
var f = document.createElement; document.createElement = function(tagName){ console.log(tagName); f.apply(document, arguments); } This code is tracking document.createElement calls dynamically, though. I.e. One cannot say about some code which tags are created beforehead - kirilloid 2012-04-04 08:18
thank you so much can you provide any tutorial which clearly explains this concep - user1275375 2012-04-04 08:44


1

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

The result is in:

str.match(re)[2];

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

2012-04-04 08:08
by mihai
This does not match var s=document.createElement(v) - user1275375 2012-04-04 08:46
In what browser - mihai 2012-04-04 08:49
it is not working because in the above code v is not in double quotes or single quotes - user1275375 2012-04-04 08:55
did you check the example? I have return the correct result for all 3 cases...I'm not sure what you mean in your commen - mihai 2012-04-04 08:59


0

Use pipe - | for OR operations.

For example: script|scipt.

2012-04-04 07:39
by Ofer Zelig
Ads