#remove a part from a string

19 messages · Page 1 of 1 (latest)

latent thorn
lime pagodaBOT

mdn String.prototype.replace()
The replace() method returns a new string with one, some, or all matches of a pattern replaced by a replacement. The pattern can be a string or a RegExp, and the replacement can be a string or a function called for each match. If pattern is a string, only the first occurrence will be replaced. The original string is left unchanged.

silk portal

<string>.replace(“<that stuff>”, “”)

dire tartan

if the number is always changing, use the regex /&s=\d{13}/gm

assuming the number is always 13 long

silk portal

ah ofc, yh

latent thorn

How would I do it when the number is not always 13 long?

dire tartan

/&s=\d+/gm

silk portal

I was just about to ask what each bit did 😂, thanks chewie

latent thorn

remove a part from a string

rotund root
let url = "https://test.com/en/editor?env=row&lat=24.89479&lon=-53.69694&s=8522367606782&zoomLevel=16";
let modifiedUrl = url.replace(/&s=[^&]*/, "");

or

let url = "https://test.com/en/editor?env=row&lat=24.89479&lon=-53.69694&s=8522367606782&zoomLevel=16";

let startIndex = url.indexOf("&s=")
let endIndex = url.indexOf("&zoomLevel")
let modifiedUrl = url.substring(0, startIndex) + url.substring(endIndex)
dire tartan

Such a shit solution lol

rotund root

but it works lol

dire tartan

The first one remove too much

And it's just overcomplicating lmao

rotund root
dire tartan

now its just mine with a different identifier

rotund root