DominiqueBal
DominiqueBal

Reputation: 797

Javascript RegEx: find GUID in an URL and replace it

I have URLs like this:

http://some.bunch.of.subdomains.hostname.tld/somepath/deeper/GUID/foo/bar/candy

GUID is the ID I need to find and replace.

The GUID is stripped, no dashes, exactly 32 characters, lowercase a-f, 0-9. Like this:

961b7d0e50f0462a8828c31bf0874a71

After I find it, I will replace it like this:

document.URL.replace(/regexp goes here/, 'newGUID');

Upvotes: 2

Views: 5234

Answers (2)

emailmuahang
emailmuahang

Reputation: 21

For regular GUID adapting from this answer https://stackoverflow.com/a/13653180/8443908

'd3aa88e2-c754-41e0-8ba6-4198a34aa0a2'.replace(/\b[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}\b/, 'newGUID');

Upvotes: 1

Qtax
Qtax

Reputation: 33928

You could try something like:

str.replace(/\b[a-f\d]{32}\b/, 'newGUID');

Upvotes: 2

Related Questions