I have the following path :
/data/2/444/test.text
or (without a slash at the start of the path)
data/2/444/test.text
I would like to return in JS the following result :
"/2/444/test.text"
I tried with the following: but I managed only to get the base name
new String(str).substring(str.lastIndexOf('/') + 1);
You can use a simple regex to remove the first directory in the path.
str.replace(/^\/?[^/]+\//, '/')
^\/? Optional slash at the beginning of string.
[^/]+\// match any non slash character until it encounter a slash
const input = ['/data/2/444/test.text', 'data/2/444/test.text', 'file.txt'];
const output = input.map(str => str.replace(/^\/?[^/]+\//, '/'))
console.log(output);
If you only want to replace /data from the beginning you can use:
^\/?data\/
lastIndexOf finds the last occurrence of a string within a string. When you use substring(x) on a string y, it will return the characters of y starting at x. So using lastIndexOf in this use case isn't what you want. You can achieve what you want by using indexOf (finding the first occurrence of a string within a string).
To account for the different formats of your input string (i.e. /data and data), you can just test for that:
function getPathWithoutData(str) {
var strWithoutSlash = str[0] === '/' ? str.substring(1) : str;
return strWithoutSlash.substring(strWithoutSlash.indexOf('/'));
}
You can easily do it without regexes and using slice and indexOf:
const getPath=path=>path.slice(path.indexOf('/',path[0]==="/"?1:0)-path.length);
console.log(getPath('/data/2/444/test.text'));
console.log(getPath('data/2/444/test.text'));
This checks if the first char is a / or not, and adjusts the indexOf accordingly to match either the second or first /. Also note how the subtraction gives a negative value, which gets the intended characters from the end, up to the /.
Of course you can do still do it with substring, as you were attempting, but with indexOf instead of lastIndexOf, because you want the 1st or 2nd / not the last one:
const getPath=path=>path.substring(path.indexOf('/',path[0]==="/"?1:0),path.length);
console.log(getPath('/data/2/444/test.text'));
console.log(getPath('data/2/444/test.text'));
It's worth mentioning that these may not be as robust as a regex, but are simple enough, and may fit your needs, depending on how the data can vary.
You could use String.prototype.split() and pass it a regex.
const paths = [
"/data/path/one",
"data/path/two"
];
const modifyPath = p => {
const [fallback, newPath] = p.split(/\/?data/);
return newPath || fallback;
}
console.log(paths.map(modifyPath));
Or you could use String.prototype.replace()
const paths = [
"/data/path/one",
"data/path/two",
];
const modifyPath = p => {
return p.replace(/\/?data(.*)/, '$1');
}
console.log(paths.map(modifyPath));
Related
I'm in non-modern JavaScript and I have a string defined as follows:
"//www.youtube.com/embed/DmYK479EpQc?vq=hd720&rel=0"
I want to pull out just the DmYK479EpQc but I don't know the length. I do know that I want what is after the / and before the ?
Is there some simple lines of JavaScript that would solve this?
Use the URL object?
console.log(
(new URL("//www.youtube.com/embed/DmYK479EpQc?vq=hd720&rel=0", location.href)).pathname
.split('/')
.pop());
Why? Because I can likely make up a URL that defeats the regex (though for youtube it's probably unlikely)
This expression might help you to do so, and it might be faster:
(d\/)([A-z0-9]+)(\?)
Graph
This graph shows how the expression would work and you can visualize other expressions in this link:
const regex = /(.*)(d\/)([A-z0-9]+)(\?)(.*)/gm;
const str = `//www.youtube.com/embed/DmYK479EpQc?vq=hd720&rel=0`;
const subst = `$3`;
// The substituted value will be contained in the result variable
const result = str.replace(regex, subst);
console.log('Substitution result: ', result);
Performance Test
This JavaScript snippet shows the performance of that expression using a simple 1-million times for loop.
const repeat = 1000000;
const start = Date.now();
for (var i = repeat; i >= 0; i--) {
const string = '//www.youtube.com/embed/DmYK479EpQc?vq=hd720&rel=0';
const regex = /(.*)(d\/)([A-z0-9]+)(\?)(.*)/gm;
var match = string.replace(regex, "$3");
}
const end = Date.now() - start;
console.log("YAAAY! \"" + match + "\" is a match 💚💚💚 ");
console.log(end / 1000 + " is the runtime of " + repeat + " times benchmark test. 😳 ");
How about non-regex way
console.log("//www.youtube.com/embed/DmYK479EpQc?vq=hd720&rel=0".split('/').pop().split('?')[0]);
I'm not going to give a piece of code because this is a relatively simple algorithm, and easy to implement.
Please note that those links has this format (correct me if I'm wrong):
https:// or http://
www.youtube.com/
embed/
Video ID (DmYK479EpQc in this case)
?parameters (note that they start ALWAYS with the character ?)
You want the ID of the video, so you can split the string into those sections and if you store those sections in one array you can be sure that the ID is at the 3rd position.
One example of how that array would look like would be:
['https://', 'www.youtube.com', 'embed', 'DmYK479EpQc', '?vq=hd720&rel=0']
One option uses a regex replacement:
var url = "//www.youtube.com/embed/DmYK479EpQc?vq=hd720&rel=0";
var path = url.replace(/.*\/([^?]+).*/, "$1");
console.log(path);
The above regex pattern says to:
.* match and consume everything up to and
/ including the last path separator
([^?]+) then match and capture any number of non ? characters
.* then consume the rest of the input
Then, we just replace with the first capture group, which corresponds to the text after the final path separator, but before the start of the query string, should the URL have one.
You can use this regex
.* match and consume everything up to
[A-z0-9]+ then match and capture any number and character between A-z
.* then consume the rest of the input
const ytUrl = '//www.youtube.com/embed/DmYK479EpQc?vq=hd720&rel=0';
const regex = /(.*)(d\/)([A-z0-9]+)(\?)(.*)/gm;
const position = '$3';
let result = ytUrl.replace(regex, position);
console.log('YouTube ID: ', result);
This regex just split the string into different sections and the YouTube id is at the 3rd position.
Another, solution is using split. This method splits a string into an array of substrings.
const ytUrl = '//www.youtube.com/embed/DmYK479EpQc?vq=hd720&rel=0';
let result = ytUrl.split('/').pop().split('?').shift()
console.log('YouTube ID: ', result);
In this sample, we split the URL using / as separator. Then we took the last element of the array with the pop method. and finally we split again using ? as separator and we take the first element of the array with the shift method.
I have 1 string in variable a which contain array parsing like variable in string format as shown below in example , I want to get all those index which is bounded by [ and ]
var a = 'info.name[0][1][5].data[0]',
collect = [];
a.split(']').reverse().forEach(function(a) {
if (a.indexOf('[') !== -1) {
var splits = a.split('[');
collect.push(splits[splits.length - 1])
}
})
console.log(collect);
my code shown above works fine I know it fails sometime so ,I am looking more better program if possible please help me to solve this problem with regular expression.
**Please Dont Use Jquery or any other libs to solve this problem **
You could use the match method:
const a = 'info.name[0][1][5].data[0]';
const collect = a.match(/[^[]+?(?=\])/g) || [];
console.log(collect);
The regular expression consists of:
[^[]+?: capture one or more characters that are not [. The ? makes that capturing stop as soon as the next condition is satisfied:
(?=\]): require that the next character is a ], but don't capture it.
The g modifier will ensure that all matches are returned.
|| [] is added for the case that there are no matches at all. In that case match returns null. This addition will output an empty array instead of that null, which may be more practical.
NB: I am not sure why you used reverse, but if you really need the output array in reversed order, you can of course apply reverse to it.
Match [, capture anything but a ] using a negative character set ([^\]]+), then match ]. Then, you can extract every captured group, which will contain the substring matched between the []s:
const a = 'info.name[0][1][5].data[0]';
const collect = [];
const re = /\[([^\]]+)\]/g;
let match;
while (match = re.exec(a)) {
collect.push(match[1]);
}
collect.reverse();
console.log(collect);
You can use this regex with match: /\b\d+\b/g
It matches digits between any non alphanumeric values such as [ and ].
const a = 'info.name[0][1][5].data[10]';
const result = a.match(/\b\d+\b/g) || [];
console.log(result);
I have a string of text that looks something like this:
?q=search&something=that&this=example/
In that example, I need to grab that . I'm using the following regex below:
var re = new RegExp("\&(.*?)\&");
Which going re[1] is giving me:
something=that - but it needs to be only that
I tried:
var re = new RegExp("\=(.*?)\&");
But that gives me everything from the first equals sign, so:
search&something=that
Is the output when it just needs to be:
that
I need to somehow target the second occurrences of 2 characters and grab whats in between them. How best do I go about this?
You can use
/something=([^&]+)/
and take the first group, see the JavaScript example:
let url = '?q=search&something=that&this=example/';
let regex = /something=([^&]+)/
let match = regex.exec(url);
console.log(match[1]);
split seems more suited to your case:
"?q=search&something=that&this=example/".split("&")[1].split("=")[1]
Then you could also implement a simple method to extract any wanted value :
function getValue(query, index) {
const obj = query.split("&")[index];
if(obj) obj.split("=")[1]
}
getValue("?q=search&something=that&this=example/", 1);
I am writing a little app for Sharepoint. I am trying to extract some text from the middle of a field that is returned:
var ows_MetaInfo="1;#Subject:SW|NameOfADocument
vti_parservers:SR|23.0.0.6421
ContentTypeID:SW|0x0101001DB26Cf25E4F31488B7333256A77D2CA
vti_cachedtitle:SR|NameOfADocument
vti_title:SR|ATitleOfADocument
_Author:SW:|TheNameOfOurCompany
_Category:SW|
ContentType:SW|Document
vti_author::SR|mrwienerdog
_Comments:SW|This is very much the string I need extracted
vti_categories:VW|
vtiapprovallevel:SR|
vti_modifiedby:SR|mrwienerdog
vti_assignedto:SR|
Keywords:SW|Project Name
ContentType _Comments"
So......All I want returned is "This is very much the string I need extracted"
Do I need a regex and a string replace? How would you write the regex?
Yes, you can use a regular expression for this (this is the sort of thing they are good for). Assuming you always want the string after the pipe (|) on the line starting with "_Comments:SW|", here's how you can extract it:
var matchresult = ows_MetaInfo.match(/^_Comments:SW\|(.*)$/m);
var comment = (matchresult==null) ? "" : matchresult[1];
Note that the .match() method of the String object returns an array. The first (index 0) element will be the entire match (here, we the entire match is the whole line, as we anchored it with ^ and $; note that adding the "m" after the regex makes this a multiline regex, allowing us to match the start and end of any line within the multi-line input), and the rest of the array are the submatches that we capture using parenthesis. Above we've captured the part of the line that you want, so that will present in the second item in the array (index 1).
If there is no match ("_Comments:SW|" doesnt appear in ows_MetaInfo), then .match() will return null, which is why we test it before pulling out the comment.
If you need to adjust the regex for other scenarios, have a look at the Regex docs on Mozilla Dev Network: https://developer.mozilla.org/en/JavaScript/Guide/Regular_Expressions
You can use this code:
var match = ows_MetaInfo.match(/_Comments:SW\|([^\n]+)/);
if (match)
document.writeln(match[1]);
I'm far from competent with RegEx, so here is my RegEx-less solution. See comments for further detail.
var extractedText = ExtractText(ows_MetaInfo);
function ExtractText(arg) {
// Use the pipe delimiter to turn the string into an array
var aryValues = ows_MetaInfo.split("|");
// Find the portion of the array that contains "vti_categories:VW"
for (var i = 0; i < aryValues.length; i++) {
if (aryValues[i].search("vti_categories:VW") != -1)
return aryValues[i].replace("vti_categories:VW", "");
}
return null;
}​
Here's a working fiddle to demonstrate.
How I can get the value after last char(. ; + _ etc.):
e.g.
string.name+org.com
I want to get "com".
Is there any function in jQuery?
Use lastIndexOf and substr to find the character and get the part of the string after it:
var extension = name.substr(name.lastIndexOf(".") + 1);
Demo: http://jsfiddle.net/Guffa/K3BWn/
A simple and readable approch to get the substring after the last occurrence of a character from a defined set is to split the string with a regular expression containing a character class and then use pop() to get the last element of the resulting array:
The pop() method removes the last element from an array and returns that element.
See a JS demo below:
var s = 'string.name+org.com';
var result = s.split(/[.;+_]/).pop();
console.log(result);
to split at all non-overlapping occurrences of the regex by default.
NOTE: If you need to match ^, ], \ or -, you may escape them and use anywhere inside the character class (e.g. /[\^\-\]\\]/). It is possible to avoid escaping ^ (if you do not put it right after the opening [), - (if it is right after the opening [, right before the closing ], after a valid range, or between a shorthand character class and another symbol): /[-^\]\\]/.
Also, if you need to split with a single char, no regex is necessary:
// Get the substring after the last dot
var result = 'string.name+org.com'.split('.').pop();
console.log(result);
Not jQuery, just JavaScript: lastIndexOf and substring would do it (not since the update indicating multiple characters). As would a regular expression with a capture group containing a character class followed by an end-of-string anchor, e.g. /([^.;+_]+)$/ used with RegExp#exec or String#match.
E.g. (live copy | source):
var match = /([^.;+_]+)$/.exec(theStringToTest),
result = match && match[1];
var s = "string.name+org.com",
lw = s.replace(/^.+[\W]/, '');
console.log(lw) /* com */
this will also work for
string.name+org/com
string.name+org.info
You can use RegExp Object.
Try this code:
"http://stackoverflow.com".replace(/.*\./,"");
I'll throw in a crazy (i.e. no RegExp) one:
var s = 'string.name+org.com';
var a = s.split('.'); //puts all sub-Strings delimited by . into an Array
var result = a[a.length-1]; //gets the last element of that Array
alert(result);​
EDIT: Since the update of the question is demanding mutiple delimiters to work this is probably not the way to go. Too crazy.....
use javascript function like
url.substr(url.length - 3);
maybe this is too late to consider, this codes works fine for me using jquery
var afterDot = value.substr(value.lastIndexOf('_') + 1);
You could just replate '_' to '.'
var myString = 'asd/f/df/xc/asd/test.jpg'
var parts = myString.split('/');
var answer = parts[parts.length - 1];
console.log(answer);