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

4
  • 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>". Commented Apr 4, 2012 at 7:37
  • what does that mean wrapping document.createElement? please explain Commented Apr 4, 2012 at 8: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. Commented Apr 4, 2012 at 8:18
  • thank you so much can you provide any tutorial which clearly explains this concept Commented Apr 4, 2012 at 8:44

2 Answers 2

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

The result is in:

str.match(re)[2];

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

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

3 Comments

This does not match var s=document.createElement(v);
it is not working because in the above code v is not in double quotes or single quotes.
did you check the example? I have return the correct result for all 3 cases...I'm not sure what you mean in your comment
0

Use pipe - | for OR operations.

For example: script|scipt.

Comments

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.