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
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