删除字符串中所有出现的文本 [英] Remove all occurrences of text within string

查看:73
本文介绍了删除字符串中所有出现的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我在JavaScript中有一个看起来像这样的字符串:

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"

即删除所有Item%5BX%5D.零件

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.

%5B和%5D之间的数字也可以是任何数字,不一定是9.

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

这看起来应该很简单,但是由于某种原因我感到困惑.我在SO上发现了一些相似的地方,但没有一个能满足上述所有条件.

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.

推荐答案

您可以使用正则表达式:

You could use a regular expression :

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

或者如果您想要更精确的信息,因为您想保留Item%6B3%4D:

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

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

这篇关于删除字符串中所有出现的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆