Replace the URL in the text with the link and replace the specified content JS code

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.