<script>
function foo() {
var bar = 'thisisvalue';
}
</script>
Hi all, I have this function in script tag and I want to get the value of var bar by using Python regular expression. Can anyone help me with this. Thanks
The pattern I always use in python is this:
import re
SEARCHER = re.compile( *regex with captured groups* )
...later, in a loop over lines...
search = SEARCHER.search(line)
if search:
value = search.group(1)
In your particular case it would be something like this:
import re
VARBAR_SEARCHER = re.compile(r"var bar = '([^']*)'")
...
search = VARBAR_SEARCHER.search(line)
if search:
value = search.group(1)
This omits the single quotes from the value. If you wanted those in there you could modify the regular expression.
var bar =won't appear elsewhere in the page?var bar =is unique