How to get "foo19" out of the following string
"C:\Users\test\AppData\Roaming\test2\storage\foo19\text.txt"
lastIndexOf does not help in this case either; I just want "foo19" which happens between two "\"s the last "\" and the one before last "\". Is it possible with regex?
Assuming foo19 is always the 2nd to last entry:
var str = "C:\\Users\\test\\AppData\\Roaming\\test2\\storage\\foo19\\text.txt";
var parts = str.split("\\");
var foo19 = parts[parts.length - 2];
Here is a regex that capture foo19 in a group, so access with $1.
.*\\(\w*)\\\w*.txt
The proof is here
Related
I want to add an element "/" within a string, but only a specific position of it, e.g. from string "20180101" to expect result like "2018/01/01".is anyone know what syntax or how to make it happen.I'm still beginner in javascript
any help would be really appreciate.
Here is one option using replace with a regex pattern:
var input = "20180419";
console.log(input.replace( new RegExp("^(\\d{4})(\\d{2})(\\d{2})", "gm"),"$1/$2/$3"));
Use substr approach to get the result:
var str = "20180101";
str = str.substr(0, str.length-4) + '/' + str.substr(4, str.length);
str = str.substr(0, str.length-2) + '/' + str.substr(7, str.length);;
console.log(str);
Another option creating an array of sub-strings with slice (note that this would work the same way with substring here), and then joining each part with a /:
let s = "20180101"
let result = [s.substring(0,4), s.substring(4,6), s.substring(6,8)].join('/')
In such cases, you may find useful to treat your string as an array of digits.
Get an array of digits from your string:
let myString = '20180101';
let myArray = myString.split('') //the '' splitter means it will just split anything
Then, add into your array of digits, the desired '/' digit in the desired positions. Refer to the splice method for more information.
myArray.splice(4,0,'/');
myArray.splice(7,0,'/');
Then, build your string:
myString = myArray.join(''); //the opposite of splitting
console.log(myString) // outputs '2018/01/01'
I am trying to use a regex to detect a pattern in the current page query string.
For this reason I have the following regex:
var re = new RegExp("([?|&])" + key + "=.*?(&|#|$)", "i");
I am testing it against this input:
http://127.0.0.1:33822/?year=2015&country=Portugal&format=psd
And it works just fine finding the pattern.
What I am really trying to get is the last character index relative to the whole URL.
So for example if I want the index o the last 'd' character:
http://127.0.0.1:33822/?year=2015&country=Portugal&format=ps**d**
In this specific case I would want 60.
Thanks in advance
You can use match.index for that. It will give you where the match exists in the original string.
Then you can use the matche[0].length to find the last character in the match
key='format'
re = new RegExp("([?|&])" + key + "=.*?(&|#|$)", "i");
url='http://127.0.0.1:33822/?year=2015&country=Portugal&format=psd'
match=url.match(re)
console.log(match.index + match[0].length-1) // 60
I have a huge string with this inside:
linha[0] = '12/2010 281R$ 272.139,05 ';
linha[0] = '13SL 1R$ 226.185,81 ';
Both lines are separate, and I need get the last occurrence from both. I'm using the following regex to match the first one:
/linha\[0]\s=\s'(.*)';/
I would like to get the second "linha..." too, but I don't know exactly how.
That's how i'm using this regex to get the first "linha...":
string.match(/linha\[0]\s=\s'(.*)';/);
output:
linha[0] = '12/2010 281R$ 272.139,05 ';
Also, i can't do extra work, i need get the second occurrence using only regex.
If you want to get the last occurrence of your regex in a string (and assuming it exists), you can do
var str = hugeString.match(/linha\[0]\s=\s'([^']*)';/g).pop();
(yes, I changed .* to [^']* for a better efficiency, ignore that if you have quotes in your inner string)
Now, if you want to extract just the submatch, you can do
var regex = /linha\[0]\s=\s'([^']*)';/g,
arr,
str;
while (arr = regex.exec(hugeString)) str = arr[1];
I have this string
"/mp3/mysong.mp3"
I need to do make this string look like this with javascript.
"/mp3/myusername/mysong.mp3"
My guess would be to find second occurrence of "/", then append "myusername/" there or prepend "/myusername" but I'm not sure how to do this in javascript.
Just capture the characters upto the second / symbol and store it into a group. Then replace the matched characters with the characters inside group 1 plus the string /myusername
Regex:
^(\/[^\/]*)
Replacement string:
$1/myusername
DEMO
> var r = "/mp3/mysong.mp3"
undefined
> r.replace(/^(\/[^\/]*)/, "$1/myusername")
'/mp3/myusername/mysong.mp3'
OR
Use a lookahead.
> r.replace(/(?=\/[^/]*$)/, "/myusername")
'/mp3/myusername/mysong.mp3'
This (?=\/[^/]*$) matches a boundary which was just before to the last / symbol. Replacing the matched boundary with /myusername will give you the desired result.
This works -
> "/mp3/mysong.mp3".replace(/(.*?\/)(\w+\.\w+)/, "$1myusername\/$2")
"/mp3/myusername/mysong.mp3"
Demo and explanation of the regex here
use this :
var str = "/mp3/mysong.mp3";
var res = str.replace(/(.*?\/){2}/g, "$1myusername/");
console.log(res);
this will insert the text myusername after the 2nd / .
I'm not very used to Javascript so I'm having trouble manipulating strings...
If I have something like /folder1/folder2/folder3/ , how do I parse it so I end up with just the current folder, e.g. "folder3" ?
Thanks!
var folderName = str.match(/(folder\d+)\/$/)[1];
Should do it.
Explanation of the regex:
( -> Start of capture group. We want a capture group because we just want
the folder name without the trailing slash.
folder -> Match the string "folder"
\d+ -> Match one or more digits
) -> End of capture group. This lets us capture strings of the form
"folderN", where N is a number.
\/ -> Escape forward slash. We have to escape this because / is used to
represent the start and end of a regex literal, in Javascript.
$ -> Match the end of the string.
The reason we are selecting the second element of the array (at index 1) is because the first element contains the complete string that was matched. This is not what we want; we just want the capture group. We only have one group that we captured, and so that is the second element.
Well, just because it's an option (though not necessarily sane):
var string = '/folder1/folder2/folder3/',
last = string.replace(/\//g,' ').trim().split(/\s/).pop();
console.log(last);
JS Fiddle demo.
You can use the split function to retrieve all subpaths:
var path = '/folder1/folder2/folder3/';
var paths = path.split('/');
var pathNeeded = paths[paths.length - 2];
Working example
How stable is the format of that string?
With a trailing slash you will need the next to last item
var parts = URL.split("/"); alert(parts[parts.length-2]);
var str = "/folder1/folder2/folder3/",
folder = str.substring(0, str.length - 1).split('/').pop();
FIDDLE
The split function transform your string into an array using the supplied parameter as a delimiter.
Therefore:
var parts = "/folder1/folder2/folder3/".split("/");
Will result in parts being equal to:
["", "folder1", "folder2", "folder3", ""]
You could then access each item using:
parts[0] // returns ''
parts[1] // returns 'folder1'
parts[2] // returns 'folder2'
.. and so on. Read more on split here:
https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/String/split
You can write the following:
var myString = '/fold1/fold2/fold3';
var myArray = myString.split('/');
var last_element = myArray[myArray.length - 1];
See the doc split