Alex Rivera | Logout

Javascript replace regex wildcard

Asked 2010-11-19T13:47:46.393
16

I have a string which I need to run a replace.

string = replace('/blogs/1/2/all-blogs/','');

The values 1, 2 and all-blogs can change. Is it possible to make them wildcards?

Thanks in advance,

Regards

Edit
Report

1 Answer

1

What about just splitting the string at slashes and just replacing the values?

var myURL = '/blogs/1/2/all-blogs/', fragments, newURL;
fragments = myURL.split('/');
fragments[1] = 3;
fragments[2] = 8;
fragments[3] = 'some-specific-blog';
newURL = fragments.join('/');

That should return:

'/blogs/3/8/some-specific-blog'
answered 2010-11-19T13:57:30.780

Your Answer