skip to Main Content

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.

2

Answers


  1. 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);
    Login or Signup to reply.
  2. This removes any character before an apostrophe and the apostrophe.

    var url = "/mode-d'emploi/slug-34";
    res = url.replace(/.'/, '');
    console.log(res);

    If you want to remove a letter only, use:

    var url = "/mode-d'emploi/slug-34";
    res = url.replace(/[a-z]'/i, '');
    console.log(res);
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search