How to remove a word from a string and all the other subsequent ones?

Asked

Viewed 48 times

1

I have a string url and I need to withdraw the word Dashboard and all subsequent characters (including bars).

Example:

"Algumcoisa/Dashboard/Outracoisa/Maisalgumacoisa"

I want you to stay like this:

"Algumacoisa/"

I’ve tried to:

pathname = window.location.pathname
url = pathname.replace("/Dashabord.*/",'')

I can always test that code on regex like the https://www.regextester.com/99144, but I can never accomplish replace.

1 answer

4


With regex (I don’t particularly like it) works with this essential modification which is to remove the double quotes that surrounds your test, because there it tests a text the way it is written and not really the word you want to search and delete from it all the rest, example:

pathname = "Algumcoisa/Dashboard/Outracoisa/Maisalgumacoisa"
url = pathname.replace(/Dashboard.*/,'');
console.log(url);

For me it is better to use a more readable form of code, first search with index the position that starts the search Dashboard and then with substring returns part of that text, example:

const url = "Algumcoisa/Dashboard/Outracoisa/Maisalgumacoisa";
const index = url.indexOf("Dashboard");
const newUrl = url.substring(0, index);
console.log(newUrl);

References:

  • Javascript - regex

    Regular expressions are patterns used to select combinations of characters in a string. In Javascript, regular expressions are also objects. They can be used with methods exec and test of the object Regexp, and with the methods match, replace, search, and split of the object String.

  • Javascript - indexof

    The index() method returns the first index where the element can be found in the array, returns -1 if it is not present.

  • Javascript - substring

    The substring() method returns a subset of a string between an Dice and another, or to the end of the string.

  • Could you explain what the code does?

  • Calm down @Codigodesenior I already know how the site works and I just explained !

  • @novic you can explain why my approach is not executed by javascript?

  • @hellysonMacedo work with regex I do not like, I do not recommend, only in the last case, because, if there are ready methods that can solve your problem, use it is quieter in its development and is even clearer for later changes. Because yours did not work I have to do a test but, must be wrong the test so did not get the expected result.

  • 1

    Well I discovered and I will also make available as a solution! @hellysonMacedo

  • @novic thank you so much for your new solution and even more for the correction you made in mine, I broke my head a lot thinking that the mistake was in regex and it never crossed my mind that it was the quotation marks.

  • 1

    Regex is so @hellysonMacedo I particularly hate to solve, but in some moments it is inevitable ..

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.