Replaces the URL in a paragraph of text entered by a user with a link address that can be clicked. For example: Http://www.111cn.net can replace [Url]http://www.cctv.com[/url]
or <a href= "http://www.cctv.com" >http://www. </a>.
The key to this is to match the link, after matching, add a tag and attributes on both sides is not a problem.
/http://[w-]* (. [ w-]*) +/ig first match http://.
[w-]* is possible www and BBS etc.
. [w-]* match. XXX form, at least one.
The test code is as follows:
The code is as follows |
Copy Code |
<script type= "Text/javascript" > function Replacereg (REG,STR) { Return Str.replace (Reg,function (m) {return ' <a href= ' +m+ ' ' > ' +m+ ' </a> ';}) } var reg =/http://[w-]* (. [ w-]*) +/ig; var str = ' Replaces the URL in a paragraph of text entered by a user with a link address that can be clicked. Test: Http://www.111cn.net is followed by Chinese and Http://bbs. is very good!http://! finally take a look at the band. CN: http://www.sina.com.cn hehe. '; document.write (Replacereg (reg,str) + ' <br/> '); </script> |
Remove the specified label from the HTML code snippet extreme content
Source: Questions about the regular
Remove <script in a piece of code .../script>
Long's regular:
The code is as follows |
Copy Code |
/< (script|meta|%) [Ss]*?/(script|meta|%) >/tried it, matching the following text normally: <script type= "Text/javascript" > I am the script to be deleted </script> |
Hey. I'm the only ones left. However, if you use a similar regular:
The code is as follows |
Copy Code |
/< (script|head|%) [Ss]*?/(script|head|%) >/ig matches a nested label: <script type= "Text/javascript" > I am the script to be deleted </script> Hey. I'm the only ones left. The actual matches are: <script type= "Text/javascript" > I am the script to be deleted </script> |
This is because [ss]*] is not a greedy cause. You can use the reverse reference in the JavaScript regular to solve the problem, and if the start tag matches the head, then the end tag must also be a. The final positive is as follows:
code is as follows |
copy code |
/ < (script|head|%) [Ss]*?/1>/ig |