Replace a string value with another string value

Viewed 68

I have a specific string /management/products.Here I'm trying to remove /management/ part from this string, So I'm expecting to return just products.

Here the code I tried,

let string = '/management/products'
string.replace(/^/management/i, "")

But this returns me a bunch of errors saying module bundle failed webpack

6 Answers

The issue is the escaping of the slashes in the regular expression. However, you do not need a regular expression at all!

With regular expression fixed:

let string = '/management/products';
string.replace(/\/management\//i, "");
// The slash is being used to end the regular expression,
// but by putting a backslash before it treats it like a regular character.

Without regular expressions at all:

let string = '/management/products';
string.replace('/management/', "");

If there are other strings other than /management/other arbitrary text here you would like this to work on, please edit your question.

You can use .split() if the part you want is always the second one.

let string = '/management/products'
string = string.split('/')[2]
console.log(string)

Can you explain why did you give /^/management/i as first argument to the replace function instead of "/management/"?

Try this,

let string = '/management/products'
string.replace("/management/", "")

Thanks

While the "split" answer with direct indexing works, you could use .at(-1) to receive the last element of your path independent of the length of it.

let string = '/management/products'
string = string.split('/').at(-1)
console.log(string)

You can use like

let string = '/management/products';
let change_str = string.replace("/management/", "");
console.log(change_str);

Visit MDN Reference

The split() method takes a pattern and divides a String into an ordered list of substrings by searching for the pattern, puts these substrings into an array, and returns the array.

Syntax

split()

split(separator)

split(separator, limit)

Parameters

separator (optional) The pattern describing where each split should occur. Can be a string or an object with a Symbol.split method — the typical example being a regular expression. If undefined, the original target string is returned wrapped in an array.

limit (optional) A non-negative integer specifying a limit on the number of substrings to be included in the array. If provided, splits the string at each occurrence of the specified separator, but stops when limit entries have been placed in the array. Any leftover text is not included in the array at all.

  • The array may contain fewer entries than limit if the end of the string is reached before the limit is reached.
  • If limit is 0, [] is returned.

Return value

An Array of strings, split at each point where the separator occurs in the given string.

let string = "/management/products"
string = string.split("/");  // Array(3): ["", "management", "products"];
string.shift(); // Removes first element which is coming to be empty.
console.log(string[0]); //management
console.log(string[1]); //products
Related