React Javascript String Edit
New here? Learn about Bountify and follow @bountify to get notified of new bounties! x

I have breadcrumbs setup for the site. The breadcrumb uses the route,

eg home > post > post1

However authentication added a token to the home URL. I want to strip away the part of the URL that has a token. Can you use the string replace to remove ALL characters after the first character of the token "$" appears?

Here is the string code, at the bottom I have added my attempt 'xxxxxxxxxx' to append characters. All I need to do is remove the character ? and anything following it (ie eliminate the token):

const convertBreadcrumb = string => {
 return string
.replace(/-/g, ' ')
.replace(/oe/g, 'ö')
.replace(/ae/g, 'ä')
.replace(/ue/g, 'ü')
.replace('?','xxxxxxxxxxxxxx');
};

Images: https://www.dropbox.com/s/hjex825qvn8pz7d/Screen%20Shot%202021-06-24%20at%2012.52.17%20PM.png?dl=0

awarded to ganganimaulik

Crowdsource coding tasks.

2 Solutions


Hello,

there's multiple ways to do this. One of them is to split the string into array and then work with the first value in that array, like this:

string.split('?')[0]

This will return all characters before '?' character, and it'll also work even if there's no '?' character.

Your function would then look like this:

const convertBreadcrumb = string => {
    return string
    .split('?')[0]
    .replace(/-/g, ' ')
    .replace(/oe/g, 'ö')
    .replace(/ae/g, 'ä')
    .replace(/ue/g, 'ü')
};
Thank you very much for your help. The reason I chose the other option was because I needed the characters before the '?'.
fullakingi 4 months ago
Winning solution

regex only solution

const convertBreadcrumb = string => {
    return string
        .replace(/-/g, ' ')
        .replace(/oe/g, 'ö')
        .replace(/ae/g, 'ä')
        .replace(/ue/g, 'ü')
        .replace(/\?.*/,'');
};
View Timeline