-3

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.

Benjamin Vbg
  • 89
  • 1
  • 12

2 Answers2

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);
Ankit Agarwal
  • 30,378
  • 5
  • 37
  • 62
1

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);
Toto
  • 89,455
  • 62
  • 89
  • 125