JavaScript 获取文本中的 URL
There are two URLs in the given text:/n/n1. 'https://url.avanan.click/v2/http://www.youtube.com.YXAzOmFtYXppbmdkaW5vOmE6bzpkM2NjMzdmMmUwM2NhNjc0ZWU3ZTdhOTM5MzBhYTdhZTo2OmM4ZDg6YTQ2MDE1MDViMDRjY2RkMTY4Y2IzMjRmZWU2YjA0MDc4MmZmMjk2NmNmYjA3MWI2YzgzOTk2MWY5ZmM2ZjQ0MzpoOlQ'/n2. 'https://url.avanan.click/v2/http://www.yahoo.com.hk.YXAzOmFtYXppbmdkaW5vOmE6bzpkM2NjMzdmMmUwM2NhNjc0ZWU3ZTdhOTM5MzBhYTdhZTo2OjYxMTI6MmNhYzZlZDQzMDdjYjkzN2Q5YzFiZWMzYmM0MDMwMTIxNWRhNTkyYTg2NzBkNjQ0N2IyZjY4Nzg2MDY4ZmFjMTpoOlQ'/n/nTo extract the URLs using JavaScript, we can use regular expressions. Here's an example:/n/n/nconst text = '...'; // the text containing the URLs/nconst regex = /https?:////[^/s]+/g;/nconst urls = text.match(regex);/n/nconsole.log(urls); // output: ['https://url.avanan.click/v2/___http://www.youtube.com___.YXAzOmFtYXppbmdkaW5vOmE6bzpkM2NjMzdmMmUwM2NhNjc0ZWU3ZTdhOTM5MzBhYTdhZTo2OmM4ZDg6YTQ2MDE1MDViMDRjY2RkMTY4Y2IzMjRmZWU2YjA0MDc4MmZmMjk2NmNmYjA3MWI2YzgzOTk2MWY5ZmM2ZjQ0MzpoOlQ', 'https://url.avanan.click/v2/___http://www.yahoo.com.hk___.YXAzOmFtYXppbmdkaW5vOmE6bzpkM2NjMzdmMmUwM2NhNjc0ZWU3ZTdhOTM5MzBhYTdhZTo2OjYxMTI6MmNhYzZlZDQzMDdjYjkzN2Q5YzFiZWMzYmM0MDMwMTIxNWRhNTkyYTg2NzBkNjQ0N2IyZjY4Nzg2MDY4ZmFjMTpoOlQ']/n/n/nThis code uses the match() method of the string object to extract all URLs in the text. The regular expression used matches any string that starts with 'http://' or 'https://' and ends with a space or a line break. The g flag at the end of the regex tells match() to find all matches, not just the first one. The resulting urls variable is an array containing the two URLs found in the text.
原文地址: https://www.cveoy.top/t/topic/oEna 著作权归作者所有。请勿转载和采集!