Remove all occurrences of text within string - 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"
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.

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

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

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

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.
JSFiddle

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

Related

Remove first folder in string

I have a folder path that always starts with a certain string which I want to remove. Let's say it looks like this:
my-bucket/2929023/32822323/file.jpg
I want it to look like this:
2929023/32822323/file.jpg
How would I do that? Thanks!
Using the functions substring and indexOf from String.prototype.
var str = "my-bucket/2929023/32822323/file.jpg";
console.log(str.substring(str.indexOf('/') + 1))
You could use a simple replace method if the string is only present once;
var string = "my-bucket/2929023/32822323/file.jpg";
var revisedString = string.replace('my-bucket/', '');
console.log(revisedString);
However, you're also able to use a Regex (regular expression) to remove it as well, something like;
var string = "my-bucket/2929023/32822323/file.jpg";
console.log(string.replace(/^my-bucket\//, ''));
Use a regex to rip the first one out. No substrings necessary.
var myString= "my-bucket/2929023/32822323/file.jpg";
myString = myString.replace(/^.+?[/]/, '');

Javascript replacing text in string with other text

I am creating a linking system that needs to be cross platform, i.e work on a webapp as well as a mobile app.
The API returns a string that looks like this,
Go to {P}Project Name{/P}
OR
Go to {F}File Name{/F}
I need to search for an occurance of {P} and {F} and replace this with a on my webapp, and then search for {/P} or {/F} and replace that with
I am trying to do this with javascript but I am coming unstuck, here is what I have so far,
var body = this.get('body');
if(body.match( \({P}|{F}/) {
}
after this I come unstuck my regex knowledge is not what it should be, can some point me in the correct direction?
One way:
str = str.replace(/({[PF]}(.*?){\/[PF]})/g, '$2');
For Go to Project Name
Consider:
str = "Go to {P}Project Name{/P}"
str = str.replace(/({P}|{F})/, '<a href="">');
str = str.replace(/({\/P}|{\/F})/, '</a>');
alert(str);
See it in action
Shorter updated solution:
str = "Go to {P}Project Name{/P}"
str = str.replace(/({P}|{F})(.*?)({\/P}|{\/F})/g, '$2');
alert(str);
See Updated DEMO
I'd recommend looking at the JS regex tuturials either at Javascript Kit or at w3schools.
In your case, you're failing to open the regex with a slash (/), and there's also the fact that brackets ({}) have a special significance on a regular expression, so you'll have to escape it.
Your regex should be something like /\{[PF]\}(.*?)\{\/[PF]\}/.
You can use indexOf (pure JS) to check whether you can find {P} or {F} in your string. Then replace them.
var str = "Go to {P}Project Name{/P}"; //this is just an example, it may contains F
if (str.indexOf("{P}" != -1} {//will return -1 if it doesn't have
str = str.replace("{P}", "<a href="">");
str = str.replace("{/P}", "</a>");
} else {
str = str.replace("{F}", "<a href="">");
str = str.replace("{/F}", "</a>");
}

Regular Expression to replace part of a string

I need to replace part of a string, it's dynamically generated so I'm never going to know what the string is.
Here's an example "session12_2" I need to replace the 2 at the end with a variable. The "session" text will always be the same but the number will change.
I've tried a standard replace but that didn't work (I didn't think it would).
Here's what I tried:
col1 = col1.replace('_'+oldnumber+'"', '_'+rowparts[2]+'"');
Edit: I'm looking for a reg ex that will replace '_'+oldnumber when it's found as part of a string.
If you will always have the "_" (underscore) as a divider you can do this:
str = str.split("_")[0]+"_"+rowparts[x];
This way you split the string using the underscore and then complete it with what you like, no regex needed.
var re = /session(\d+)_(\d+)/;
var str = 'session12_2';
var subst = 'session$1_'+rowparts[2];
var result = str.replace(re, subst);
Test: https://regex101.com/r/sH8gK8/1

Regex to capture both '?' and '%3f' in a javascript replace() method

I'm terrible with Regex, can't find a suitable answer on Stack which works for this.
I have a string like this:
var str = 'abc?def%3f999%3F^%&$*'
I only want to remove the following:
?, %3f and %3F (the entity codes for question marks)
I've tried this:
var theQuery = str.replace([\\?]|\\%3f|\\%3F,'');
But this doesn't appear to be valid regex. What's a solution that will work here?
You can use this:
var str = 'abc?def%3f999%3F^%&$*'
var theQuery = str.replace(/\?|%3f/gi, '');
//=> abcdef999^%&$*
You need to use regex delimiters / and /
You need to use global switch g
No need to double escape
No need to escape %

match end of line javascript regex

I'm probably doing something very stupid but I can't get following regexp to work in Javascript:
pathCode.replace(new RegExp("\/\/.*$","g"), "");
I want to remove // plus all after the 2 slashes.
Seems to work for me:
var str = "something //here is something more";
console.log(str.replace(new RegExp("\/\/.*$","g"), ""));
// console.log(str.replace(/\/\/.*$/g, "")); will also work
Also note that the regular-expression literal /\/\/.*$/g is equivalent to the regular-expression generated by your use of the RegExp object. In this case, using the literal is less verbose and might be preferable.
Are you reassigning the return value of replace into pathCode?
pathCode = pathCode.replace(new RegExp("\/\/.*$","g"), "");
replace doesn't modify the string object that it works on. Instead, it returns a value.
This works fine for me:
var str = "abc//test";
str = str.replace(/\/\/.*$/g, '');
alert( str ); // alerts abc
a = a.replace(/\/\/.*$/, "");

Categories