Valuk
Valuk

Reputation: 1636

Remove all occurrences of text within string

Say I had a string in JavaScript that looked like this:

var str = "Item%5B9%5D.Something%5B0%5D.Prop1=1&Item%5B9%5D.Something%5B0%5D.Prop2=False&Item%5B9%5D.Something%5B0%5D.Prop3=10%2F04%2F2013+00%3A00%3A00&Item%5B9%5D.Something%5B1%5D.Prop1=2&Item%5B9%5D.Something%5B1%5D.Prop2=False&Item%5B9%5D.Something%5B1%5D.Prop3=10%2F04%2F2013+00%3A00%3A00&Item%5B9%5D.Something%5B2%5D.Prop1=3&Item%5B9%5D.Something%5B2%5D.Prop2=False&Item%5B9%5D.Something%5B2%5D.Prop3=29%2F04%2F2013+00%3A00%3A00&Item%5B9%5D.Something%5B3%5D.Prop1=4&Item%5B9%5D.Something%5B3%5D.Prop2=False&Item%5B9%5D.Something%5B3%5D.Prop3=29%2F04%2F2013+00%3A00%3A00"

and wanted it to look like this:

var str = "Something%5B0%5D.Prop1=1&Something%5B0%5D.Prop2=False&Something%5B0%5D.Prop3=10%2F04%2F2013+00%3A00%3A00&Something%5B1%5D.Prop1=2&Something%5B1%5D.Prop2=False&Something%5B1%5D.Prop3=10%2F04%2F2013+00%3A00%3A00&Something%5B2%5D.Prop1=3&Something%5B2%5D.Prop2=False&Something%5B2%5D.Prop3=29%2F04%2F2013+00%3A00%3A00&Something%5B3%5D.Prop1=4&Something%5B3%5D.Prop2=False&Something%5B3%5D.Prop3=29%2F04%2F2013+00%3A00%3A00"

i.e. remove all of the Item%5BX%5D. parts

How would I go about doing this? I thought of using something like:

str = str.substring(str.indexOf('Something'), str.length);

but obviously that only removes the first occurrence.

Also the number in-between the %5B and %5D could be anything, not necessarily 9.

This seems like something that should be simple but for some reason I'm stumped. I found a few similarish things on SO but nothing that handled all the above criteria.

Upvotes: 3

Views: 5837

Answers (6)

solanki...
solanki...

Reputation: 5098

Using split() & join() method

var str = "Item%5B9%5D.Something%5B0%5D.Prop1=1&Item%5B9%5D.Something%5B0%5D.Prop2=False&Item%5B9%5D.Something%5B0%5D.Prop3=10%2F04%2F2013+00%3A00%3A00&Item%5B9%5D.Something%5B1%5D.Prop1=2&Item%5B9%5D.Something%5B1%5D.Prop2=False&Item%5B9%5D.Something%5B1%5D.Prop3=10%2F04%2F2013+00%3A00%3A00&Item%5B9%5D.Something%5B2%5D.Prop1=3&Item%5B9%5D.Something%5B2%5D.Prop2=False&Item%5B9%5D.Something%5B2%5D.Prop3=29%2F04%2F2013+00%3A00%3A00&Item%5B9%5D.Something%5B3%5D.Prop1=4&Item%5B9%5D.Something%5B3%5D.Prop2=False&Item%5B9%5D.Something%5B3%5D.Prop3=29%2F04%2F2013+00%3A00%3A00";
console.log(str.split(/Item%5B\d%5D\./g).join(''));

Upvotes: 0

RaulentRoi
RaulentRoi

Reputation: 29

Avoid using a regular expression where complex "needle" escaping is required:

var str =  "something complex full of http://, 'quotes' and more keep1 something complex full of http://, 'quotes' and more keep2 something complex full of http://, 'quotes' and more keep3"
var needle = "something complex full of http://, 'quotes' and more";
 while( str.indexOf(needle) != '-1')
 str = str.replace(needle,"");
 document.write(str);

Outputs: keep1 keep2 keep3

Upvotes: 2

Denys Séguret
Denys Séguret

Reputation: 382150

You could use a regular expression :

str = str.replace(/Item[^.]+\./g, '');

or if you want something more precise because you'd want to keep Item%6B3%4D :

str = str.replace(/Item%5B.%5D\./g, '');

Upvotes: 7

Habib
Habib

Reputation: 223267

str = str.replace('Item%5B9%5D', '');

EDIT: Missed the part where 9 in the string could be any number. You can use:

str = str.replace(/Item%5B\d%5D\./g, '');

Upvotes: 4

MMM
MMM

Reputation: 7310

Try using regular expressions:

str = str.replace(/Item%5B[^.]*%5D./g, '');

This assumes that you can have anything of any length between %5B and %5D.

JSFiddle

Upvotes: 1

Vishal Suthar
Vishal Suthar

Reputation: 17194

Here you go:

str = str.replace(/Item%5B\d%5D\./g,'');

Live Demo

Upvotes: 1

Related Questions