I have a function that I thought I had working to replace a URL with an HTML link when someone submits something on a form of ours. If the users enters a URL with a space, for example:
company.sharepoint.com/sites/testsite/shared documents/forms/allitems.aspx
The function cuts off anything after the space.
Below is my code:
ReplaceURLwithHTMLLinks: function(text) {
if (text !=null && text.length > 0) {
var exp = /(\b(https?):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|}{\[\]\(\)"'])/i;
if (text.indexOf("-") > -1) {
var linkText;
if (text.indexOf("company.sharepoint.com") > -1
linkText = text.substring(0, text.indexOf('-') - 1) + " - SharePoint";
else
linkText = text.substring(0, text.indexOf('-') - 1);
var newLink = text.replace(exp, "<a target='_blank' rel='noopener noreferrer' href='$1'>" + linkText + "</a>";
return newLink.substring(newLink.indexOf("<a"));
}
}
else {
return test;
}
}
I am trying to find out how to replace any blank spaces with a %20 to still allow the HTML Link to load properly
Thanks
You could try this?
text.replace(/\s/g, "%20");
it replaces all the empty spaces with %20