eugeneK
eugeneK

Reputation: 11126

How to replace using wildcards in Javascript

str = str.replace('?dfsdfsf=fdsfd.fdsfds/', 'uploads/')

i want to replace in url everything from '?' to first appearance of '/' afterwards ( including ) with 'uploads/'

instead of https://websitename.com/?qqfile=somestrangefilename.jpguploads/d6113e4a-e789-44cd-8ba7-9684d29116dd.jpg

i want to get https://websitename.com/uploads/d6113e4a-e789-44cd-8ba7-9684d29116dd.jpg

Upvotes: 2

Views: 7165

Answers (3)

Shef
Shef

Reputation: 45589

var str = '?dfsdfsf=fdsfd.fdsfds/';
str = str.replace(/(\?[^\/]+\/)/, 'uploads/');

Check it live

Upvotes: 2

Molecular Man
Molecular Man

Reputation: 22386

you can use regexp

str.replace(/\?[^\?\/]*\//, 'uploads/')

Upvotes: 7

Bojangles
Bojangles

Reputation: 101513

You'll need to use a regular expression for this.

Try:

str = str.replace(/\?(.+)\//i, 'uploads/');

Explanation:

\? Finds the first ?.
(.+) Finds any amount of any non-newline character.
\/ Finds the last /
/i Tells the regular expression to be case insensitive, just for good measure.

Upvotes: 3

Related Questions