Avatar of MJ
MJ
Flag for United States of America asked on

Append String to Page Name when Extension Isn't Necessarily Mandatory?

What  I want to do is append "-confirmation" to the URL of a page (but using output as a string). It will always be before the file extension, if one exists? Since the extension isn't mandatory, the URL can have two different structures. Not sure how to accomplish this?

 Example (==> means returns):

https://www.abc.com/dir1/dir2/lead-form.html    ==> https://www.abc.com/dir1/dir2/lead-form-confirmation.html
https://www.abc.com/dir1/dir2/lead-form    ==> https://www.abc.com/dir1/dir2/lead-form-confirmation
https://tech.internal.abc.com/dir1/webpage.html    ==> https://tech.internal.abc.com/dir1/webpage-confirmation.html
https://tech.internal.abc.com/dir1/webpage   ==> https://tech.internal.abc.com/dir1/webpage-confirmation

Any ideas?

Thanks!
JavaScript

Avatar of undefined
Last Comment
Pawan Kumar

8/22/2022 - Mon
Leonidas Dosas

function returnHtmlstr(arg){ 
  var string = arg,
    expr = /.html/; 
  if(string.search(expr)){
    string=string.replace('.html','');
    string=string+'-confirmation.html';
    string=string.replace(' ','');
    return string;
  }
}

console.log(returnHtmlstr('https://tech.internal.abc.com/dir1/webpage.html'));

Open in new window

ASKER CERTIFIED SOLUTION
Pawan Kumar

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy