简体   繁体   English

删除字符串中所有出现的文本

[英]Remove all occurrences of text within string

Say I had a string in JavaScript that looked like this:假设我在 JavaScript 中有一个如下所示的字符串:

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"

ie remove all of the Item%5BX%5D.即删除所有 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.此外,%5B 和 %5D 之间的数字可以是任何数字,不一定是 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.我在 SO 上发现了一些相似的东西,但没有任何东西可以处理上述所有标准。

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 :或者如果你想要更精确的东西,因为你想保留Item%6B3%4D

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

EDIT: Missed the part where 9 in the string could be any number.编辑:错过了字符串中9可以是任何数字的部分。 You can use:您可以使用:

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

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输出:keep1 keep2 keep3

Here you go:干得好:

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

Live Demo现场演示

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 .这假设您可以拥有%5B%5D之间的任何长度。

JSFiddle JSFiddle

Using split() & join() method使用split()join()方法

 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(''));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM