Given a few scenarios, how can I match and extract alphanumeric characters (and symbols) within a String containing URLs? I'm currently using Google Apps Script for retrieving a plain body text of a hyperlinked text from a Gmail thread message, and I'd basically like to match and extract the title out of some Strings as follows:
var scenario1 = "Testing: Stack Overflow Title 123? https://www.stackoverflow.com";
... in which I'd like to only output: "Testing: Stack Overflow Title 123?"
Here's another scenario:
var scenario2 = "https://www.stackoverflow.com Testing: Stack Overflow Title 123? https://www.stackoverflow.com";
... again, in which I'd like to only output: "Testing: Stack Overflow Title 123?"
I've tried the following for initially testing to see if the String first contains a URL (in which I confirmed that the regex for matching URLs works and outputs: https://www.stackoverflow.com), and then tests to see if a title exists to eventually extract it, but to no avail:
var scenario1 = "Testing: Stack Overflow Title 123? https://www.stackoverflow.com";
var scenario2 = "https://www.stackoverflow.com Testing: Stack Overflow Title 123? https://www.stackoverflow.com";
var urlRegex = /(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/;
var titleRegex = /^[a-zA-Z0-9_:?']*$/;
var containsUrl = urlRegex.test(element);
if (containsUrl) {
var containsTitle = titleRegex.test(scenario1);
if (containsTitle) { // No match, and doesn't run
var title = titleRegex.exec(element)[0];
Logger.log("title: " + title);
}
}
Basically, I'd like a Regex pattern that matches EVERYTHING but URLs, if possible