Search code examples
javascriptphpregexurlslug

Find and remove apostrophe and one letter before?


The goal is to clean up a character string and delete elements not important for the user and SEO ie the (letter before the apostrophes) in my case. I would mainly like a solution or an explanation of a regex that would do that in PHP but the logic is no different in JS.

character string : .../mode-d'emploi/...

Become:

url: /mode-emploi/slug-34

Maybe someone can help me.


Solution

  • You can split with the apostrophe and then remove the last character of the first splitted string so that it simulates the removal of character just before apostrophe:

    var url = "/mode-d'emploi/slug-34";
    
    var resArray = url.split("\'");
    resArray[0] = resArray[0].substring(0, resArray[0].length - 1);
    var res = resArray.join('');
    console.log(res);