Is it possible to change a certain string within an URL with another term using script?
After you've created an string out of your url as Simone said in his comment, you should find the ${pageName}_${languageName}
word; Based on the details you've provided, you can do it like this:
// creating an string out of url; Note that you should define createUrlString function yourself
const urlString = createUrlString(url);
//finding the page_language word in the string
const wordsArray = urlString.split('/');
const pageLanguage = wordsArray.find(word => word.includes('_'))
After this step, you can easily change the 'pageLanguage' word by replacing current language with the selected one, and then replace it in the url string.