简体   繁体   中英

Best way to count hyperlinks in textarea using Javascript client-side?

We have a textarea control that holds text and hyperlinks. The links are stored as follows:

http://www.google.com [Link to a site __title__ Title of Link]
http://www.yahoo.com [http://www.yahoo.com __title__ Link with text & hyperlink the same]

In the second link, I don't want to count yahoo twice, so I want to ignore links starting with the left bracket. I know that using Regex to do this isn't the best way, but I don't know of any other way to accomplish this. So far I've tried this regex, but I figured out that Javascript doesn't support lookbehind:

(?<!\[)((http|https|ftp)\://(www\.)?)(([a-zA-Z0-9\.-]+\.[a-zA-Z]{2,4})|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(/[a-zA-Z0-9%:/_\?\.'~&=-]*)

Anyone know of a decent way to accomplish this?

I just found out also that I can't rely on the brackets. Users can enter any type of link, using our tool that creates the brackets or by just entering it manually themselves.

Count the number of the character [ in the textarea contents.
The above solution will work, if the format that you have mentioned above persists.

You just need a count? Can't you count all the links then count all the links starting with the left bracket, and subtract?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM