Remove first folder in string - javascript

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(/^.+?[/]/, '');

Related

Get a substring from a string for a regular expression in JavaScript

I have a string of the following form:
data-translate='view-7631b26ea80b1b601c313b15cc4e2ab03faedf30'>Avatar data
It can be in different languages, but in any case I need to get a string which is between the characters ' '
That is, in the example above, I need to get the following string:
view-7631b26ea80b1b601c313b15cc4e2ab03faedf30
Can I do this using the method string.replace(regexp, str) ?
I've highlighted the desired line using the following regular expression:
/'\b(.*)\b'/gm
Now, using the method string.replace I need to delete everything except that...
Got any suggestions?
Use match method.
var data = "data-translate='view-7631b26ea80b1b601c313b15cc4e2ab03faedf30'>Avatar data";
data = data.match(/'\b(.*)\b'/gm)
You have good solid anchor text in either side, so:
var match = /data-translate='([^']+)'/.exec(str);
var substr = match && match[1];
Live Example:
var str = "data-translate='view-7631b26ea80b1b601c313b15cc4e2ab03faedf30'>Avatar data";
var match = /data-translate='([^']+)'/.exec(str);
var substr = match && match[1];
document.body.innerHTML =
"<pre>Got: [" + substr + "]</pre>";
But again, as I said in a comment, using a simple regular expression to extract information from HTML is usually doomed to fail. For instance, you probably don't want to match this:
<p>The string is data-translate='view-7631b26ea80b1b601c313b15cc4e2ab03faedf30'</p>
...and yet, a simple regex solution will do exactly that. To properly handle HTML, you must use a proper HTML parser.
You can also try this one:
/\'([^\']+)\'/gm

Delete Substring From String Using Regexp in Javascript

I have a string like this
string = '/location/start:2015-06-06/end:2015-06-06'
and I would substitute the start and end paramaters.
Therefore, I would like to delete everything like start:2015-06-06 and end:2015-06-06 by using Regexp.
But I do not know how.
string = string.replace(/start:[\d-]*/g,"").replace(/end:[\d-]*/g,"");
This should work:
var str = '/location/start:2015-06-06/end:2015-06-06';
var result = str.replace(/start:[0-9]{4}-[0-9]{2}-[0-9]{2}/, 'something');

string.Replace in AngularJs

With c# there is a string.Replace-method.
Like This:
string oldString = "stackoverflow";
string newString= oldString.Replace("stackover","");
Output: flow
Can I do something similar to this with AngularJs?
My try doesn't work:
var oldString = "stackoverflow";
$scope.newString= oldString.Replace("stackover","NO");
In Javascript method names are camel case, so it's replace, not Replace:
$scope.newString = oldString.replace("stackover","NO");
Note that contrary to how the .NET Replace method works, the Javascript replace method replaces only the first occurrence if you are using a string as first parameter. If you want to replace all occurrences you need to use a regular expression so that you can specify the global (g) flag:
$scope.newString = oldString.replace(/stackover/g,"NO");
See this example.
The easiest way is:
var oldstr="Angular isn't easy";
var newstr=oldstr.toString().replace("isn't","is");
var oldString = "stackoverflow";
var str=oldString.replace(/stackover/g,"NO");
$scope.newString= str;
It works for me.
Use an intermediate variable.

Javascript RegEx global string search for underscore character(_)

I am horrible with RegEx and I have been using this online tester for some time now and still can not find what I need.
So I have the string "2011_G-20_Cannes_summit". I want to replace all the underscores (_) with spaces.
So I want something like this:
var str = "2011_G-20_Cannes_summit";
str.replace(/_/g," "); or str.replace(/\_/g);
Though neither is working...
What am I missing?
That works fine. The replace method doesn't modify the existing string, it creates a new one. This will do what you want:
var str = "2011_G-20_Cannes_summit";
str = str.replace(/_/g," ");

Javascript replace

Hello struggling here guys..
Is it possible to string replace anything between the the first forward slashes with "" but keep the rest?
e.g. var would be
string "/anything-here-this-needs-to-be-replaced123/but-keep-this";
would end up like this
string "/but-keep-this";
Hope that made sence
You can simply split it by /
var str = "/anything-here-this-needs-to-be-replaced123/but-keep-this";
var myarray = str.split('/');
alert('/' . myarray[2]);
var s = "/anything-here-this-needs-to-be-replaced123/but-keep-this";
pos = s.lastIndexOf("/");
var s2 = s.substring(pos);
alert(s2);
Like this:
var string = "/anything-here-this-needs-to-be-replaced123/but-keep-this";
string = string.substring(string.indexOf('/', 1));
You can view a demo here to play with, the .indexOf() method takes an optional second argument, saying where to start the search, just use that with .substring() here.
If you want to remove all leading slashes (unclear from the example), change it up a bit to .lastIndexOf() without a start argument, like this:
var string = "/anything-here-this-needs-to-be-replaced123/but-keep-this";
string = string.substring(string.lastIndexOf('/'));
You can play with that here, the effect is the same for the example, but would be different in the case of more slashes.

Categories