如何在html中获取函数属性?

时间:2021-08-21 22:29:09

edit: yayy,this is the question that got me banned. thanks to the community.

编辑:yayy,这是让我被禁止的问题。感谢社区。

I am trying to capture the function redirecttoauth with a regular expression, but it doesn't do what I expect.

我试图用正则表达式捕获函数redirecttoauth,但它不能达到我的预期。

start = 'function redirecttoauth(sessionid'
end = '}'
result = re.search('%s(.*)%s' % (start, end), s).group(1)
print(result)

and tried to escape } and ) but it didn't work.

并试图逃脱}和)但它没有奏效。

1 个解决方案

#1


1  

regex is really the wrong way to do that. for a simple example your approach might work, you need the DOTALL flag though and you need to escape the parantheses:

正则表达式真是错误的做法。举一个简单的例子你的方法可能有效,你需要DOTALL标志,你需要逃避这些问题:

import re
s = r'''<html>
 <head>
  <script type="text/javascript">
  function redirecttoauth(sessionid, test) {
   function body ...
  }
  </script>
 </head>
 <body>
 </body>
</html>
'''

start = r'function redirecttoauth\(sessionid'
end = r'}'
result = re.search(r'%s(.*)%s' % (start, end), s, re.DOTALL).group(1)
print(result)

#1


1  

regex is really the wrong way to do that. for a simple example your approach might work, you need the DOTALL flag though and you need to escape the parantheses:

正则表达式真是错误的做法。举一个简单的例子你的方法可能有效,你需要DOTALL标志,你需要逃避这些问题:

import re
s = r'''<html>
 <head>
  <script type="text/javascript">
  function redirecttoauth(sessionid, test) {
   function body ...
  }
  </script>
 </head>
 <body>
 </body>
</html>
'''

start = r'function redirecttoauth\(sessionid'
end = r'}'
result = re.search(r'%s(.*)%s' % (start, end), s, re.DOTALL).group(1)
print(result)