Js Regular Expression judgment and deletion hyperlink

Source: Internet
Author: User

After sorting out two instances online, you can determine whether there are links in the strings and filter them. You can also directly Delete All hyperlinks in the text. Let's take a look at the instances.

Delete link directly

The Code is as follows: Copy code

/**
* Delete hyperconnections in text
* @ Param {string}
* @ Returns {string}
*/
DelStrLink: function (str ){
Var htmlreg =/(</? A [^>] *> )(?!. * 1)/ig; // Delete the regular expression used by the hyperjoin.
Str = str. replace (htmlreg ,'');
Return str;
}

If it is more advanced, we can determine whether the string has links.

The Code is as follows: Copy code

<Script>
Var txtContent = 'I'm a few hyperlink http://www.hzhuti.com, http://www.bKjia. c0m/, www.baidu.com ';
Var httpReg = new RegExp ("(http [s] {} | ftp): // [a-zA-Z0-9 \. \-] + \. ([a-zA-Z] {2, 4}) (: \ d + )? (/[A-zA-Z0-9 \.\-~! @ # $ % ^ & Amp; * +? : _/= <>] *)? "," Gi "); // used to determine the text content hyperlink
// The text address is changed to a link
FormatTxtContent = txtContent. replace (httpReg, function (httpText ){
Return '<a href = "' + httpText + '" target = "_ blank">' + httpText + '</a> ';
});
Document. write ('original text: '+ txtContent );
Document. write ('<br/> ');
Document. write ('formatted text: '+ formatTxtContent );
</Script>

Related Article

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.