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开发者_如何学JAVA 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?
精彩评论